首頁 > 後端開發 > php教程 > WordPress 外掛程式繼續文件:物件導向程式設計 II

WordPress 外掛程式繼續文件:物件導向程式設計 II

王林
發布: 2023-08-29 15:10:01
原創
872 人瀏覽過

WordPress 插件继续文档:面向对象编程 II

在本系列的這一點上,我們已準備好透過記錄我們的檔案、類別、函數、變數等來完善我們的插件。

雖然這是我們實際上必須完成插件的最後一步,但這並不是該系列的最後一篇文章,因為我們將繼續研究物件導向程式設計中的一些高級主題。

但在此之前,讓我們將我們在上一篇文章中學到的所有內容付諸實踐,將我們的插件升級到 1.0 版本。

當然,與之前的所有文章一樣,我建議您及時了解我們迄今為止介紹的所有內容,這樣您不僅可以完全了解我們所做的工作在上一篇文章中,但我們實際上是如何達到本文討論的最後一點的。

  1. 簡介
  2. 課程
  3. 類型
  4. 控制結構:條件語句
  5. 控制結構:循環
  6. 函數與屬性
  7. 範圍
  8. 建構外掛 I
  9. 建立外掛 II
  10. 記錄外掛 I

了解和審查了所有這些內容後,讓我們開始記錄每個文件。

記錄外掛

我們可以透過多種不同的方式來記錄這個外掛:

  • 我們可以先記錄所有檔案頭,然後我們可以回來記錄類,然後我們可以回來記錄變量,然後我們可以記錄函數。
  • 我們可以一次記錄每個文件,並對每個文件中包含的所有內容進行簡短的討論。

顯然,該選項將為每個部分生成更多文檔,但應該會導致一篇不那麼乏味的文章,並且更容易理解整個插件的控制流程。

為此,我們將逐個文件地研究該插件,為我們擁有的每段程式碼引入文檔,然後我們將討論程式碼後面的任何感興趣的點。

最後,我們將確保在文章末尾引用該外掛程式的最終版本。話雖如此,讓我們開始吧。

單一帖子元管理器

回想一下,啟動插件的主檔案是位於插件目錄根目錄中的 single-post-meta-manager.php 檔案。

以下是該文件的完整文件版本。仔細閱讀每則評論,不僅要注意其遵循的格式,還要注意其提供的內容。

<?php
/**
 * The file responsible for starting the Single Post Meta Manager plugin
 *
 * The Single Post Meta Manager is a plugin that displays the post meta data
 * associated with a given post. This particular file is responsible for
 * including the necessary dependencies and starting the plugin.
 *
 * @package SPPM
 *
 * @wordpress-plugin
 * Plugin Name:       Single Post Meta Manager
 * Plugin URI:        https://github.com/tommcfarlin/post-meta-manager
 * Description:       Single Post Meta Manager displays the post meta data associated with a given post.
 * Version:           1.0.0
 * Author:            Tom McFarlin
 * Author URI:        http://tommcfarlin.com
 * Text Domain:       single-post-meta-manager-locale
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Domain Path:       /languages
 */

// If this file is called directly, then about execution.
if ( ! defined( 'WPINC' ) ) {
    die;
}

/**
 * Include the core class responsible for loading all necessary components of the plugin.
 */
require_once plugin_dir_path( __FILE__ ) . 'includes/class-single-post-meta-manager.php';

/**
 * Instantiates the Single Post Meta Manager class and then
 * calls its run method officially starting up the plugin.
 */
function run_single_post_meta_manager() {

	$spmm = new Single_Post_Meta_Manager();
	$spmm->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。我們將在整個插件中使用它。

包含目錄

接下來,讓我們將注意力轉向包含目錄中的所有檔案。

由於在管理目錄中的任何內容之前都需要這些文件,因此單獨查看這些文件中的每一個是有意義的,然後用管理目錄中的其余文件來完善我們的討論。

單一帖子元管理器

<?php

/**
 * The Single Post Meta Manager is the core plugin responsible for including and
 * instantiating all of the code that composes the plugin
 *
 * @package SPMM
 */

/**
 * The Single Post Meta Manager is the core plugin responsible for including and
 * instantiating all of the code that composes the plugin.
 *
 * The Single Post Meta Manager includes an instance to the Single Post Manager
 * Loader which is responsible for coordinating the hooks that exist within the
 * plugin.
 *
 * It also maintains a reference to the plugin slug which can be used in
 * internationalization, and a reference to the current version of the plugin
 * so that we can easily update the version in a single place to provide
 * cache busting functionality when including scripts and styles.
 *
 * @since    1.0.0
 */
class Single_Post_Meta_Manager {

    /**
	 * A reference to the loader class that coordinates the hooks and callbacks
	 * throughout the plugin.
	 *
	 * @access protected
	 * @var    Single_Post_Meta_Manager_Loader   $loader    Manages hooks between the WordPress hooks and the callback functions.
	 */
	protected $loader;

	/**
	 * Represents the slug of hte plugin that can be used throughout the plugin
	 * for internationalization and other purposes.
	 *
	 * @access protected
	 * @var    string   $plugin_slug    The single, hyphenated string used to identify this plugin.
	 */
	protected $plugin_slug;

	/**
	 * Maintains the current version of the plugin so that we can use it throughout
	 * the plugin.
	 *
	 * @access protected
	 * @var    string   $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Instantiates the plugin by setting up the core properties and loading
	 * all necessary dependencies and defining the hooks.
	 *
	 * The constructor will define both the plugin slug and the verison
	 * attributes, but will also use internal functions to import all the
	 * plugin dependencies, and will leverage the Single_Post_Meta_Loader for
	 * registering the hooks and the callback functions used throughout the
	 * plugin.
	 */
	public function __construct() {

		$this->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;
	}

}
登入後複製

顯然,這個特定文件中引入了很多新評論;但是,每個類別屬性、建構子和內部函數的作用應該是非常不言自明的。

除瞭如何透過外掛程式協調資訊之外,需要注意的關鍵是我們如何遵守上一篇文章中定義的標準。

但請注意,如果文件中的某些標籤和/或功能在不相關時使用,我們就冒昧地使用它們。我們將在本文的其餘部分中繼續這樣做。

單一帖子元管理器載入器

<?php

/**
 * The Single Post Meta Manager Loader is a class that is responsible for
 * coordinating all actions and filters used throughout the plugin
 *
 * @package    SPMM
 */

/**
 * The Single Post Meta Manager Loader is a class that is responsible for
 * coordinating all actions and filters used throughout the plugin.
 *
 * This class maintains two internal collections - one for actions, one for
 * hooks - each of which are coordinated through external classes that
 * register the various hooks through this class.
 *
 * @since    1.0.0
 */
class Single_Post_Meta_Manager_Loader {

    /**
	 * A reference to the collection of actions used throughout the plugin.
	 *
	 * @access protected
	 * @var    array    $actions    The array of actions that are defined throughout the plugin.
	 */
	protected $actions;

	/**
	 * A reference to the collection of filters used throughout the plugin.
	 *
	 * @access protected
	 * @var    array    $actions    The array of filters that are defined throughout the plugin.
	 */
	protected $filters;

	/**
	 * Instantiates the plugin by setting up the data structures that will
	 * be used to maintain the actions and the filters.
	 */
	public function __construct() {

		$this->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 類別具有單一職責:定義為儀表板呈現元框及其樣式的功能。

<?php

/**
 * The Single Post Meta Manager Admin defines all functionality for the dashboard
 * of the plugin
 *
 * @package SPMM
 */

/**
 * The Single Post Meta Manager Admin defines all functionality for the dashboard
 * of the plugin.
 *
 * This class defines the meta box used to display the post meta data and registers
 * the style sheet responsible for styling the content of the meta box.
 *
 * @since    1.0.0
 */
class Single_Post_Meta_Manager_Admin {

    /**
	 * A reference to the version of the plugin that is passed to this class from the caller.
	 *
	 * @access private
	 * @var    string    $version    The current version of the plugin.
	 */
	private $version;

	/**
	 * Initializes this class and stores the current version of this plugin.
	 *
	 * @param    string    $version    The current version of this plugin.
	 */
	public function __construct( $version ) {
		$this->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';
	}

}
登入後複製

请注意,上面的类只有很少的功能细节。主要是,该类维护对插件版本的引用、用于设置元框样式的样式表以及实际渲染元框所需的函数。

回想一下,所有这些都是在核心插件文件和加载器中设置的。这有助于解耦插件中存在的逻辑,以便每个类都可以专注于其主要目的。

当然,插件的最后一部分依赖于包含显示元框所需标记的实际部分文件。

单个帖子元管理器部分

<?php
/**
 * Displays the user interface for the Single Post Meta Manager meta box.
 *
 * This is a partial template that is included by the Single Post Meta Manager
 * Admin class that is used to display all of the information that is related
 * to the post meta data for the given post.
 *
 * @package    SPMM
 */
?>
<div id="single-post-meta-manager">

    <?php $post_meta = get_post_meta( get_the_ID() ); ?>
	<table id="single-post-meta-manager-data">
	<?php foreach ( $post_meta as $post_meta_key => $post_meta_value ) { ?>
		<tr>
			<td class="key"><?php echo $post_meta_key; ?></td>
			<td class="value"><?php print_r( $post_meta_value[0] ); ?></td>
		</tr>
	<?php } ?>
	</table>

</div><!-- #single-post-meta-manager -->
登入後複製

这应该是相对不言自明的;但是,为了完整起见,请注意此文件获取当前帖子 ID(通过使用 get_the_ID() 函数),读取帖子元数据,然后迭代它构建一个表显示键和值。

完成插件

至此,我们已经完成了插件的实现。从实施面向对象的编程实践,到记录代码。

您可以在 GitHub 上获取该插件的最终版本;但是,我们将在更多帖子中继续进行面向对象的讨论,以便我们可以探索一些更高级的主题,例如继承、抽象和其他主题。

同时,如果您对该插件有疑问或意见,请随时在评论中留下!

以上是WordPress 外掛程式繼續文件:物件導向程式設計 II的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板