Created
May 25, 2024 20:03
-
-
Save drazenbebic/4680c415371bb57e2a9997dc44a64a51 to your computer and use it in GitHub Desktop.
Self-Hosted WordPress Plugin Updates (Client Plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Self-Hosted WordPress Plugin Updates - Client | |
Description: Demo plugin showcasing a client plugin which updates from a custom update server. | |
Version: 1.0.0 | |
Author: Drazen Bebic | |
Author URI: https://drazen.bebic.dev | |
Text Domain: shwpuc | |
Domain Path: /languages | |
*/ | |
// Current plugin version. | |
define( "SHWPUC_PLUGIN_VERSION", "1.0.0" ); | |
// Output of this will be | |
// "self-hosted-plugin-updates/self-hosted-plugin-updates.php". | |
define( "SHWPUC_PLUGIN_SLUG", plugin_basename( __FILE__ ) ); | |
// Set the server base URL. This should | |
// be replaced with the actual URL of | |
// your update server. | |
define( "SHWPUC_API_BASE_URL", "https://example.com/wp-json/shwpus/v1" ); | |
/** | |
* Returns the plugin slug: self-hosted-plugin-updates | |
* | |
* @return string | |
*/ | |
function shwpuc_get_plugin_slug() { | |
// We split this string because we need the | |
// slug without the fluff. | |
list ( $t1, $t2 ) = explode( '/', SHWPUC_PLUGIN_SLUG ); | |
// This will remove the ".php" from the | |
// "self-hosted-plugin-updates.php" string | |
// and leave us with the slug only. | |
return str_replace( '.php', '', $t2 ); | |
} | |
/** | |
* Add our self-hosted auto-update plugin | |
* to the filter transient. | |
* | |
* @param $transient | |
* | |
* @return object $transient | |
*/ | |
function shwpuc_check_for_update( $transient ) { | |
// This will be "self-hosted-plugin-updates-client" | |
$slug = shwpuc_get_plugin_slug(); | |
// Set the server base URL. This should be replaced | |
// with the actual URL of your update server. | |
$api_base = SHWPUC_API_BASE_URL; | |
// This needs to be obtained from the | |
// site settings. Somewhere set by a | |
// setting your plugin provides. | |
$license_i_surely_paid_for = 'XXX-YYY-ZZZ'; | |
// Get the remote version. | |
$remote_version = shwpuc_get_remote_version( $slug ); | |
// This is the URL the new plugin | |
// version will be downloaded from. | |
$download_url = "$api_base/package/$slug.$remote_version.zip?license=$license_i_surely_paid_for"; | |
// If a newer version is available, add the update. | |
if ( $remote_version | |
&& version_compare( SHWPUC_PLUGIN_VERSION, $remote_version, '<' ) | |
) { | |
$obj = new stdClass(); | |
$obj->slug = $slug; | |
$obj->new_version = $remote_version; | |
$obj->url = $download_url; | |
$obj->package = $download_url; | |
$transient->response[ SHWPUC_PLUGIN_SLUG ] = $obj; | |
} | |
return $transient; | |
} | |
// Define the alternative API for updating checking | |
add_filter( 'pre_set_site_transient_update_plugins', 'shwpuc_check_for_update' ); | |
/** | |
* Return the latest version of a plugin on | |
* the remote update server. | |
* | |
* @return string|null $remote_version | |
*/ | |
function shwpuc_get_remote_version( $slug ) { | |
$api_base = SHWPUC_API_BASE_URL; | |
$license = 'XXX-YYY-ZZZ'; | |
$url = "$api_base/version/$slug?license=$license"; | |
$request = wp_remote_get( $url ); | |
if ( ! is_wp_error( $request ) | |
|| wp_remote_retrieve_response_code( $request ) === 200 | |
) { | |
return $request['body']; | |
} | |
return null; | |
} | |
/** | |
* Add our self-hosted description to the filter | |
* | |
* @param boolean $false | |
* @param array $action | |
* @param stdClass $arg | |
* | |
* @return bool|stdClass | |
*/ | |
function shwpuc_check_info( $false, $action, $arg ) { | |
// This will be "self-hosted-plugin-updates" | |
$slug = shwpuc_get_plugin_slug(); | |
// Abort early if this isn't our plugin. | |
if ( $arg->slug !== $slug ) { | |
return false; | |
} | |
// Set the server base URL. This should be replaced | |
// with the actual URL of your update server. | |
$api_base = SHWPUC_API_BASE_URL; | |
$license = 'XXX-YYY-ZZZ'; | |
$url = "$api_base/info/$slug?license=$license"; | |
$request = wp_remote_get( $url ); | |
if ( ! is_wp_error( $request ) | |
|| wp_remote_retrieve_response_code( $request ) === 200 | |
) { | |
return unserialize( $request['body'] ); | |
} | |
return null; | |
} | |
// Define the alternative response for information checking | |
add_filter( 'plugins_api', 'shwpuc_check_info', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment