tarecord/wp_commercial_updater

一个用于在不使用WordPress.org仓库的情况下更新商业WordPress插件的库

dev-main 2020-10-30 20:20 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:04 UTC


README

一个用于WordPress插件中的抽象类,它可以挂钩到WordPress更新检查以定义替代的包URL。使用此库,您可以无缝地更新您的商业插件(该插件不在WordPress.org仓库中),同时在仓库中的插件旁边更新。

用法

使用composer安装此库

composer require tarecord/wp_commercial_updater

在您的插件中,扩展该类并实现所需的方法(get_latest_version()get_url()get_package_url()

示例

<?php
use Tarecord\WP_Commercial_Updater;

class TAR_Updater extends WP_Commercial_Updater {

	/**
	 * Return the latest version number of the plugin.
	 *
	 * @return string
	 */
	protected function get_latest_version()
	{
        // Do anything you need to provide the latest version number.
        // for example: perform a wp_remote_get() request to a Github repository retrieving the latest release or call out to your own server that hosts the plugin zip.
		return '1.0.0';
	}

	/**
	 * Get the plugin url.
	 *
	 * @return string The URL.
	 */
	protected function get_url()
	{
		return 'https://github.com/tarecord/my-plugin';
	}

	/**
	 * Get the package url.
	 *
	 * @return string The package URL.
	 */
	protected function get_package_url()
	{
		return 'https://github.com/tarecord/my-plugin/archive/v1.0.0.zip';
	}

}

实现所需的方法后,使用init()方法在您的插件中初始化该类。

// Initialize your extension of the class passing in the current plugin version and slug.
$updater = new TAR_Updater( '1.0.0', 'my-super-cool-plugin' );

// Initialize the class which sets up the filters for `transient_update_plugins` and `site_transient_update_plugins`
$updater->init();

使用私有服务器

如果您计划使用自己的服务器托管插件,以下是如何实现此目标的示例。

<?php
use Tarecord\WP_Commercial_Updater;

class TAR_Updater extends WP_Commercial_Updater {
    /**
     * Get plugin version from distribution server.
     * 
     * @return string|WP_Error The plugin version or an instance of WP_Error.
     */
    protected function get_latest_version() {
        $response = wp_remote_get(
            'https://plugins.example.com/my-plugin/details.json',
            array(
                'headers' => array(
                    'Authorization' => 'Basic '. base64_encode( 'your-username' . ':' . 'your-password')
                )
            )
        );

        if ( ! is_wp_error( $response ) ) {
            $data = json_decode( wp_remote_retrieve_body( $response ) );
            return 
        } else {
            error_log( $response->get_error_message );
        }

        return $response;
    }

    /**
	 * Get the plugin url.
	 *
	 * @return string The URL.
	 */
	protected function get_url()
	{
		return 'https://example.com/my-plugin';
	}

	/**
	 * Get the package url.
	 *
	 * @return string The package URL.
	 */
	protected function get_package_url()
	{
		return 'https://plugins.example.com/my-plugin/latest.zip';
	}
}