使用 Envato WordPress 工具包增強您的主題:庫

使用 Envato WordPress 工具包增強您的主題:庫

在本教程的最后一部分中,我們學習了如何使用 TGM 插件激活類來在任何時候都需要 Envato WordPress 工具包插件我們的主題正在使用中。該插件允許用戶在管理員中安裝和更新購買的主題。

下一部分將教您如何實現 Envato WordPress 工具包庫,以便我們可以使用 Envato Marketplace API 定期檢查我們的主題何時有可用更新。

當有更新可用時,我們將在管理員中顯示通知,并引導用戶訪問插件進行更新。


1.?包括工具包庫

我們首先需要將工具包庫包含在我們的項目中。下載 Envato WordPress 工具包庫 ZIP 文件。解壓縮并將文件夾 envato-wordpress-toolkit-library 復制到主題中的 inc 文件夾中。您最終應該得到這些路徑:

  • mytheme/inc/envato-wordpress-toolkit-library/class-envato-protected-api.php
  • mytheme/inc/envato-wordpress-toolkit-library/class-envato-wordpress-theme-upgrader.php

注意:您可以更改上述文件的位置以滿足您的需要?;蛘?,您可以從本文頂部的下載鏈接下載完整源代碼。


2.?管理員掛鉤函數

現在我們可以開始編碼了。我們將掛鉤 admin_init 操作。將以下代碼添加到您的 functions.php 中:

 /**  * Load the Envato WordPress Toolkit Library check for updates  * and direct the user to the Toolkit Plugin if there is one  */ function envato_toolkit_admin_init() {      // Include the Toolkit Library     include_once( get_template_directory() . '/inc/envato-wordpress-toolkit-library/class-envato-wordpress-theme-upgrader.php' );      // Add further code here  } add_action( 'admin_init', 'envato_toolkit_admin_init' ); 

3.?使用工具包插件信息

工具包庫需要 Envato 用戶名和 API 密鑰才能工作。由于我們在上一個教程中需要 Toolkit 插件,因此我們可以使用在其設置中找到的用戶名和 API 密鑰字段的輸入值。如果這些字段未填寫,我們可以顯示一條通知,要求用戶在 Toolkit 插件中輸入它們。

 // Use credentials used in toolkit plugin so that we don't have to show our own forms anymore $credentials = get_option( 'envato-wordpress-toolkit' ); if ( empty( $credentials['user_name'] ) || empty( $credentials['api_key'] ) ) {     add_action( 'admin_notices', 'envato_toolkit_credentials_admin_notices' );     return; } 

我們需要在外部添加相應的鉤子函數來顯示我們的管理通知:

 /**  * Display a notice in the admin to remind the user to enter their credentials  */ function envato_toolkit_credentials_admin_notices() {     $message = sprintf( __( "To enable theme update notifications, please enter your Envato Marketplace credentials in the %s", "default" ),         "<a href="%22%20.%20admin_url()%20.%20%22admin.php?page=envato-wordpress-toolkit">Envato WordPress Toolkit Plugin</a>" );     echo "<div id="message" class="updated below-h2"><p>{$message}</p></div>"; } 

4.定期更新檢查

工具包庫始終使用 Envato Marketplace API 檢查主題更新。這并不好,因為每次用戶訪問管理頁面時都執行它會顯著減慢頁面加載時間。我們只需要定期檢查更新即可。

每 3 小時檢查一次聽起來是個好主意:

 // Check updates only after a while $lastCheck = get_option( 'toolkit-last-toolkit-check' ); if ( false === $lastCheck ) {     update_option( 'toolkit-last-toolkit-check', time() );     return; }  // Check for an update every 3 hours if ( 10800  <hr><h2> <span>5.</span>?檢查更新</h2> <p>最后,我們可以使用該庫檢查更新:</p> <pre class="brush:php;toolbal:false;"> // Check for updates $upgrader = new Envato_WordPress_Theme_Upgrader( $credentials['user_name'], $credentials['api_key'] ); $updates = $upgrader-&gt;check_for_theme_update();  // If $updates-&gt;updated_themes_count == true then we have an update! 

6.主題更新通知

從這一刻起,您可以選擇使用工具包庫函數 $upgrader->upgrade_theme(); 自動更新主題,但是,我相信給用戶一個選擇通常是一個好主意。

我的建議是只顯示主題更新的通知,并允許用戶使用 Toolkit 插件進行更新:

 // Add update alert, to update the theme if ( $updates-&gt;updated_themes_count ) {     add_action( 'admin_notices', 'envato_toolkit_admin_notices' ); } 

我們需要在當前函數之外顯示顯示通知的函數:

 /**  * Display a notice in the admin that an update is available  */ function envato_toolkit_admin_notices() {     $message = sprintf( __( "An update to the theme is available! Head over to %s to update it now.", "default" ),         "<a href="%22%20.%20admin_url()%20.%20%22admin.php?page=envato-wordpress-toolkit">Envato WordPress Toolkit Plugin</a>" );     echo "<div id="message" class="updated below-h2"><p>{$message}</p></div>"; } 

為什么首先使用該插件?

您可能認為也可以只使用 Toolkit 庫而不使用 Toolkit 插件,然后也許只在主題選項中顯示我們自己的用戶名和 API 密鑰表單.

雖然這是完全可能做到的,但使用該插件給我們帶來了一些好處:

  1. 在全新主題激活中,如果之前已安裝 Toolkit 插件,我們的主題將自動檢查更新。
  2. 我們不需要添加一組額外的表單供用戶填寫。
  3. 用戶可以自行更新 Toolkit 插件,因此 Marketplace API 中的更改可以應用于 WordPress 實例,而無需任何主題指導。

結論

就是這樣!我們集成了 Envato WordPress 工具包插件和庫來檢查主題更新?,F在,一旦市場中有更新可用,我們的客戶就會收到管理員通知。我們所做的最好的事情是他們可以輕松執行更新而無需離開管理員。

您可以繼續從文章上方的鏈接下載完整的源代碼。該代碼還包含本教程前一部分中涵蓋的主題。

由于這是我的第一個教程系列,我非常感謝任何反饋、評論和建議。讓我知道你的想法!

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