我正在开发一个游戏,我正在计划,我将有合作伙伴,这样他们就可以运行像我这样的游戏。
为此,我开发了一个插件,包含一些类和函数,用于处理游戏请求。
因为这个插件对于所有wordpress用户来说都是完全不可用的,所以我想用一封压缩格式的电子邮件将这个插件发送给我的合作伙伴,但我希望他们能够升级插件,如果他们需要的话。
那么,有没有可能将插件存储在我自己的服务器上,并强制插件从我的站点检查更新,而不是从wp plugins目录检查更新?
我正在开发一个游戏,我正在计划,我将有合作伙伴,这样他们就可以运行像我这样的游戏。
为此,我开发了一个插件,包含一些类和函数,用于处理游戏请求。
因为这个插件对于所有wordpress用户来说都是完全不可用的,所以我想用一封压缩格式的电子邮件将这个插件发送给我的合作伙伴,但我希望他们能够升级插件,如果他们需要的话。
那么,有没有可能将插件存储在我自己的服务器上,并强制插件从我的站点检查更新,而不是从wp plugins目录检查更新?
以下职位的所有学分归Abid Omar. 完整的教程可以在Tuts+上找到:A Guide to the WordPress HTTP API: Automatic Plugin Updates
<要点是创建一个新类,该类将发布一个外部文件(API),并在插件过期时返回下载插件。下面假设您正在使用以下代码创建插件(不在functions.php
或mu-plugins
目录)。wp_autoupdate.php
将是一个外部文件(在插件中),包含下面定义的自动更新类update.php
是用于检查更新的外部“远程”文件,创建APIadd_action( \'init\', \'wptuts_activate_au\' );
function wptuts_activate_au()
{
require_once (\'wp_autoupdate.php\'); // File which contains the Class below
$wptuts_plugin_current_version = \'1.0\';
$wptuts_plugin_remote_path = \'http://localhost/update.php\';
$wptuts_plugin_slug = plugin_basename(__FILE__);
new wp_auto_update( $wptuts_plugin_current_version, $wptuts_plugin_remote_path, $wptuts_plugin_slug );
}
Autoupdate Class - wp_autoupdate.php
class wp_auto_update
{
/**
* The plugin current version
* @var string
*/
public $current_version;
/**
* The plugin remote update path
* @var string
*/
public $update_path;
/**
* Plugin Slug (plugin_directory/plugin_file.php)
* @var string
*/
public $plugin_slug;
/**
* Plugin name (plugin_file)
* @var string
*/
public $slug;
/**
* Initialize a new instance of the WordPress Auto-Update class
* @param string $current_version
* @param string $update_path
* @param string $plugin_slug
*/
function __construct( $current_version, $update_path, $plugin_slug )
{
// Set the class public variables
$this->current_version = $current_version;
$this->update_path = $update_path;
$this->plugin_slug = $plugin_slug;
list ($t1, $t2) = explode(\'/\', $plugin_slug);
$this->slug = str_replace( \'.php\', \'\', $t2 );
// define the alternative API for updating checking
add_filter( \'pre_set_site_transient_update_plugins\', array( &$this, \'check_update\' ) );
// Define the alternative response for information checking
add_filter(\'plugins_api\', array(&$this, \'check_info\'), 10, 3);
}
/**
* Add our self-hosted autoupdate plugin to the filter transient
*
* @param $transient
* @return object $ transient
*/
public function check_update( $transient )
{
if( empty( $transient->checked ) ) {
return $transient;
}
// Get the remote version
$remote_version = $this->getRemote_version();
// If a newer version is available, add the update
if ( version_compare( $this->current_version, $remote_version, \'<\' ) ) {
$obj = new stdClass();
$obj->slug = $this->slug;
$obj->new_version = $remote_version;
$obj->url = $this->update_path;
$obj->package = $this->update_path;
$transient->response[$this->plugin_slug] = $obj;
}
var_dump( $transient );
return $transient;
}
/**
* Add our self-hosted description to the filter
*
* @param boolean $false
* @param array $action
* @param object $arg
* @return bool|object
*/
public function check_info( $false, $action, $arg )
{
if( $arg->slug === $this->slug ) {
$information = $this->getRemote_information();
return $information;
}
return false;
}
/**
* Return the remote version
* @return string $remote_version
*/
public function getRemote_version()
{
$request = wp_remote_post( $this->update_path, array( \'body\' => array( \'action\' => \'version\' ) ) );
if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
return $request[\'body\'];
}
return false;
}
/**
* Get information about the remote version
* @return bool|object
*/
public function getRemote_information()
{
$request = wp_remote_post( $this->update_path, array( \'body\' => array( \'action\' => \'info\' ) ) );
if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200) {
return unserialize( $request[\'body\'] );
}
return false;
}
/**
* Return the status of the plugin licensing
* @return boolean $remote_license
*/
public function getRemote_license()
{
$request = wp_remote_post( $this->update_path, array( \'body\' => array( \'action\' => \'license\' ) ) );
if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
return $request[\'body\'];
}
return false;
}
}
Remote ( External ) Update API - update.php
if( isset( $_POST[\'action\'] ) ) {
switch( $_POST[\'action\'] ) {
case \'version\':
echo \'1.1\';
break;
case \'info\':
$obj = new stdClass();
$obj->slug = \'plugin.php\';
$obj->plugin_name = \'plugin.php\';
$obj->new_version = \'1.1\';
$obj->requires = \'3.0\';
$obj->tested = \'3.3.1\';
$obj->downloaded = 12540;
$obj->last_updated = \'2012-01-12\';
$obj->sections = array(
\'description\' => \'The new version of the Auto-Update plugin\',
\'another_section\' => \'This is another section\',
\'changelog\' => \'Some new features\'
);
$obj->download_link = \'http://localhost/update.php\';
echo serialize( $obj );
case \'license\':
echo \'false\';
break;
}
} else {
header( \'Cache-Control: public\' );
header( \'Content-Description: File Transfer\' );
header( \'Content-Type: application/zip\' );
readfile( \'update.zip\' );
}
<小时>update.php
将在插件上保留最新信息。使用init
函数,您可以传递当前安装的版本,以便它可以将其发布到update.php
并对照电流进行检查。代码还有一些改进的空间,可以使其更加精简,但我使用它来完成您想要做的事情,所以这是一个很好的切入点。是的,您可以托管自己的插件,并让插件检查来自服务器的更新。您需要在插件中添加一些自定义代码,让它与服务器一起检查更新。有一些框架可以帮助您做到这一点,请尝试以下方法:
知道为什么下面的代码function my_plugin_load() { get_plugins(); } add_action( \'plugins_loaded\', \'my_plugin_load\' ); 抛出此错误?Fatal error: 不应调用未定义的函数get\\u plugins()get_plugins() 定义在plugins_loaded 胡克开火了?如果不是,那么什么才是合适的钩子呢?(这个钩子应该启动插件的引导/加载过程)