在您的網站上展示 WordPress.org 插件的信息

在您的網站上展示 WordPress.org 插件的信息

在本文的第一部分中,我們討論了如何使用內置函數與 WordPress.org 進行通信并檢索插件詳細信息。

在本教程中,我們將把理論付諸實踐,創建一個簡單的插件,該插件將允許我們使用簡碼在我們的 WordPress 網站上顯示 WordPress.org 上托管的任何插件的詳細信息。 p>


開始使用

我假設您是插件開發人員并且了解基礎知識,但如果有疑問,我建議閱讀以下兩篇文章:

  • 開發 WordPress 插件的兩種方法:函數式編程
  • 開發 WordPress 插件的兩種方法:面向對象編程

我們在做什么?

通過這個插件,我們想要創建一個短代碼,例如 [mpi slug=’my-plugin-information’ field=’version’] ,它可以接受兩個屬性:“slug”和“field”,然后基于然后,我們檢索并顯示 WordPress.org 存儲庫中托管的任何插件的信息。

創建插件庫

讓我們首先在 wp-content/plugins 目錄中創建一個名為 my-plugin-information 的文件夾。在其中創建一個名為 my-plugin-info.php 的文件,并將以下代碼粘貼到其中:

 <?php /* Plugin Name: My Plugin Info Plugin URI: https://myplugininfo.com Description: Communicate with WordPress.org Plugins API to retrive your Plugin Information Version: 0.1 Author: Harish Author Email: mye@email.com License: GPL3 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly  if ( ! class_exists( 'DOT_MyPluginInfo' ) ) {  	class DOT_MyPluginInfo {  		/** 		 * Constructor 		 */ 		function __construct() {  			//Hook up to the init action 			add_action( 'init', array( &$this, 'init_my_plugin_info' ) );  		}  		/** 		 * Runs when the plugin is initialized 		 */ 		function init_my_plugin_info() {  			// Register the shortcode [mpi slug='my-plugin-info' field='version'] 			add_shortcode( 'mpi', array( &$this, 'render_mpi' ) );  		}  		function render_mpi($atts) {  		}  	} // end class  	new DOT_MyPluginInfo(); } ?>

我們做了什么?

在上面的代碼中,我們創建并初始化了插件類 DOT_MyPluginInfo。其中包含任何插件的通用塊,例如 __construct() 方法。

函數 init_my_plugin_info 與 init 操作掛鉤,以便它在加載 WordPress 之后但在發送任何標頭之前運行。在函數 init_my_plugin_info 中,我們使用 add_shortcode 函數注冊了我們的短代碼。

注意:要了解有關 add_shortcode 的更多信息,請查看 Codex。

上面的插件現在有足夠的代碼可以被 WordPress 從插件儀表板識別。如果您已經按照說明創建了文件,您現在可以訪問插件頁面并激活此插件。


設置簡碼

由于我們希望靈活地選擇要顯示有關插件的信息,因此我們創建了一個具有兩個屬性的短代碼。第一個稱為“slug”將用于指定需要檢索哪個插件的數據。第二個屬性“field”將用于指定我們需要顯示的插件的具體信息。

例如,如果我們想顯示該插件的下載次數,我們只需在帖子編輯器下方添加文本,最終結果應該是“下載了 100 次”之類的內容。 p>

 Downloaded [mpi slug='my-plugin-information' field='downloaded'] times. 

使用 add_shortcode 我們注冊了我們的短代碼,以便每當在帖子內容中找到短代碼時,都會調用函數 render_mpi() 來處理它。從現在開始,其余代碼將放置在該函數內來處理我們的短代碼。

使用 render_mpi() 處理短代碼

要顯示插件信息,我們首先需要處理短代碼以獲取屬性。在 render_api 函數中添加以下代碼:

 // get our variable from $atts extract( shortcode_atts( array( 	'slug' =&gt; '', //foo is a default value 	'field' =&gt; '' ), $atts ) ); 

這會提取兩個屬性“slug”和“field”(如果提供)。在繼續之前,我們首先檢查“slug”和“field”的值是否存在,如果不存在,則停止進一步處理。

 /**  * Check if slug exists  */ if ( ! $slug ) { 	return false; }  /**  * Check if field exists  * Return value based on the field attribute  */ if ( ! $field ) { 	return false; } else {  } // $field check 

上面的代碼將檢查“slug”是否存在,如果不存在,則返回 false。如果“slug”確實存在,它將繼續檢查“field”屬性。由于我們只是創建一個短代碼來顯示有關插件的特定信息,因此在進一步處理之前檢查這兩個屬性是否存在將節省對 WordPress.org 插件 API 的不必要的調用。

現在,如果短代碼中提供了“slug”和“field”屬性的值,我們將首先清理這兩個值。

 // Sanitize attributes $slug = sanitize_title( $slug ); $field = sanitize_title( $field ); 

在瞬態中存儲插件數據

為了避免每次加載包含此短代碼的頁面時都向 WordPress.org 發送請求,我們需要在本地保存插件信息。這樣,如果您放置了多個短代碼來顯示同一插件的不同詳細信息,我們就可以通過顯示您網站上本地保存的信息中的數據來加快這一過程。

但是如果插件更新并且我們繼續顯示舊數據怎么辦?為了解決這個問題,最快的選擇是使用 Transients API 保存我們的個人插件數據并設置到期日期數據。

另一個問題是,如果您有正在檢索有關不同插件的數據的短代碼。如果我們使用單個臨時名稱存儲它們,結果可能會出乎意料。為了解決這個問題,我們使用“slug”屬性為保存的瞬態提供一個唯一的名稱。

為什么要經歷這一切?

  • 單獨保存每個插件的信息
  • 減少向 WordPress.org 發出的請求
  • 通過直接從您自己的網站提供數據來更快地加載數據

讓我們首先創建一個變量 $mpi_transient_name 來保存基于“slug”屬性的唯一瞬態名稱。

 // Create a empty array with variable name different based on plugin slug $mpi_transient_name = 'mpi-' . $slug; 

接下來我們檢查瞬態是否已經存在:

 /**  * Check if transient with the plugin data exists  */  $mpi_info = get_transient( $mpi_transient_name ); 

如果瞬態存在,我們將繼續根據“field”屬性顯示數據,否則我們使用 plugins_api 連接到 WordPress.org 并請求插件信息。

 if ( empty( $mpi_info ) ) {  	/** 	 * Connect to WordPress.org using plugins_api 	 * About plugins_api - 	 * https://code.tutsplus.com/tutorials/communicating-with-the-wordpress-org-plugin-api--wp-33069 	 */ 	require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); 	$mpi_info = plugins_api( 'plugin_information', array( 'slug' =&gt; $slug ) );  	// Check for errors with the data returned from WordPress.org 	if ( ! $mpi_info or is_wp_error( $mpi_info ) ) { 		return false; 	}  	// Set a transient with the plugin data 	// Use Options API with auto update cron job in next version. 	set_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );  } 

在上面的代碼中,我們做了三件事:

  1. 我們連接到 WordPress.org 并請求插件信息。然后該請求被保存在名為 $mpi_info 的變量中
  2. 然后我們進行錯誤檢查,以確保返回的數據是否沒有錯誤
  3. 最后,我們創建了一個過期日期為一小時的新瞬態

現在,如果 slug 屬性的值為“my-plugin-information”,那么存儲插件信息的瞬態名稱將為“mpi-my-plugin-information”。

注意:要了解有關 plugins_api 的更多信息,請參閱本系列的第一篇文章,如本文頂部所示。

顯示插件信息

最后一步涉及根據“field”屬性的值返回特定信息。為此,我們只需使用單獨的檢查即可。

 if ( $field == "downloaded" ) { 	return $mpi_info-&gt;downloaded; }  if ( $field == "name" ) { 	return $mpi_info-&gt;name; }  if ( $field == "slug" ) { 	return $mpi_info-&gt;slug; }  if ( $field == "version" ) { 	return $mpi_info-&gt;version; }  if ( $field == "author" ) { 	return $mpi_info-&gt;author; }  if ( $field == "author_profile" ) { 	return $mpi_info-&gt;author_profile; }  if ( $field == "last_updated" ) { 	return $mpi_info-&gt;last_updated; }  if ( $field == "download_link" ) { 	return $mpi_info-&gt;download_link; } 

總結

完整的插件代碼:

 <?php /* Plugin Name: My Plugin Information Plugin URI: https://code.tutsplus.com Description: Communicate with WordPress.org Plugins API to retrive your Plugin Information Version: 0.1.1 Author: Harish Author Email: me@email.com License:    Copyright 2013 Harish    This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License, version 3, as   published by the Free Software Foundation.    This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.    You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly  if ( ! class_exists( 'DOT_MyPluginInfo' ) ) {  	class DOT_MyPluginInfo {  		/** 		 * Constructor 		 */ 		function __construct() {  			//Hook up to the init action 			add_action( 'init', array( &$this, 'init_my_plugin_info' ) );  		}  		/** 		 * Runs when the plugin is initialized 		 */ 		function init_my_plugin_info() {  			// Register the shortcode [mpi slug='my-plugin-info' field='version'] 			add_shortcode( 'mpi', array( &$this, 'render_mpi' ) );  		}  		function render_mpi($atts) {  			// get our variable from $atts 			extract(shortcode_atts(array( 				'slug' => '', //foo is a default value 				'field' =&gt; '' 			), $atts));  			/** 			 * Check if slug exists 			 */ 			if ( ! $slug ) { 				return false; 			}  			/** 			 * Check if field exists 			 * Return value based on the field attribute 			 */ 			if ( ! $field ) {  				return false;  			} else {  				// Sanitize attributes 				$slug = sanitize_title( $slug ); 				$field = sanitize_title( $field );  				// Create a empty array with variable name different based on plugin slug 				$mpi_transient_name = 'mpi' . $slug;  				/** 				 * Check if transient with the plugin data exists 				 */ 				$mpi_info = get_transient( $mpi_transient_name );  				if ( empty( $mpi_info ) ) {  					/** 					 * Connect to WordPress.org using plugins_api 					 * About plugins_api - 					 * https://code.tutsplus.com/tutorials/communicating-with-the-wordpress-org-plugin-api--wp-33069 					 */ 					require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); 					$mpi_info = plugins_api( 'plugin_information', array( 'slug' =&gt; $slug ) );  					// Check for errors with the data returned from WordPress.org 					if ( ! $mpi_info or is_wp_error( $mpi_info ) ) { 						return false; 					}  					// Set a transient with the plugin data 					// Use Options API with auto update cron job in next version. 					set_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );  				}  				if ( $field == "downloaded" ) { 					return $mpi_info-&gt;downloaded; 				}  				if ( $field == "name" ) { 					return $mpi_info-&gt;name; 				}  				if ( $field == "slug" ) { 					return $mpi_info-&gt;slug; 				}  				if ( $field == "version" ) { 					return $mpi_info-&gt;version; 				}  				if ( $field == "author" ) { 					return $mpi_info-&gt;author; 				}  				if ( $field == "author_profile" ) { 					return $mpi_info-&gt;author_profile; 				}  				if ( $field == "last_updated" ) { 					return $mpi_info-&gt;last_updated; 				}  				if ( $field == "download_link" ) { 					return $mpi_info-&gt;download_link; 				}  			} // $field check  		} // render_mpi()  	} // end class 	new DOT_MyPluginInfo();  }  ?&gt; 

此插件代碼可在 github 上找到,您也可以從 WordPress.org 下載


付諸行動

現在您只需轉到帖子編輯器并添加一個短代碼,例如:

 Downloaded [mpi slug='my-plugin-information' field='downloaded'] times. 

它會顯示:

 Downloaded 10 times. 

顯示有關插件的其他信息的示例簡碼

通過替換“field”屬性的值,您可以顯示不同的信息,例如:

  • 插件名稱:[mpi slug=’my-plugin-information’ field=’name’]
  • 插件版本:[mpi slug=’my-plugin-information’ field=’version’]
  • 插件 Slug:[mpi slug=’my-plugin-information’ field=’slug’]
  • 插件作者(返回名稱和鏈接):[mpi slug=’my-plugin-information’ field=’author’]
  • 作者簡介(返回個人資料地址):[mpi slug=’my-plugin-information’ field=’author_profile’]
  • 最后更新:[mpi slug=’my-plugin-information’ field=’last_updated’]
  • 下載鏈接:[mpi slug=’my-plugin-information’ field=’download_link’]

改進

為了簡單起見,我使用瞬態來保存插件信息。然而,瞬態從來就不是用來保存重要數據的。另一種方法是使用選項 API、add_options() 或作為 post meta 保存插件數據,然后安排一個 cron 任務每小時更新一次數據。


接下來做什么?

使用 plugins_api,我們已經演示了通信和檢索 WordPress.org 上托管的任何插件的信息是多么容易。

您可能還想查看其他插件,例如 Plugin Info(也使用 plugins_api 和 I Make Plugins,看看它們如何完成相同的任務。

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享