Skip to content

Instantly share code, notes, and snippets.

@btxtiger
Last active March 11, 2024 01:15
Show Gist options
  • Save btxtiger/3cfbb62d4e6d9e4a65e52d603e30f922 to your computer and use it in GitHub Desktop.
Save btxtiger/3cfbb62d4e6d9e4a65e52d603e30f922 to your computer and use it in GitHub Desktop.
Update WordPress Plugin from GitHub Releases
<?php
/**
* Retrieve updates from GitHub
* Add the following snipped to your main wordpress plugin file
* Note that the public github API has a rate limit of 60 per hour per IP
*/
add_filter('site_transient_update_plugins', function($transient) {
// Config
$wpPluginSlug = 'my-wordpress-plugin';
$githubRepoSlug = '<user>/<repository_name>';
$githubRepoUrl = "https://github.com/$githubRepoSlug";
$githubReleasesUrl = "https://api.github.com/repos/$githubRepoSlug/releases";
$cacheFilename = 'github-releases.json';
$cacheCreatedFilename = 'github-checked-at.txt';
if (empty($transient->checked)) {
return $transient;
}
// Check if the cache file exists
$cacheFile = plugin_dir_path(__FILE__) . $cacheFilename;
$cacheCreatedFile = plugin_dir_path(__FILE__) . $cacheCreatedFilename;
// Check if cache is older than 1 hour
$isCacheOld = false;
if (file_exists($cacheCreatedFile)) {
$cacheCreated = file_get_contents($cacheCreatedFile);
$cacheCreatedTimestamp = (int)$cacheCreated;
$currentTimestamp = time();
$diff = $currentTimestamp - $cacheCreatedTimestamp;
if ($diff > 3600) {
$isCacheOld = true;
}
}
if (file_exists($cacheFile) && !$isCacheOld) {
$releaseData = json_decode(file_get_contents($cacheFile), true);
} else {
// Fetch the release data from GitHub
$response = wp_remote_get($githubReleasesUrl, [
'timeout' => 10,
'headers' => [
'Accept' => 'application/vnd.github.v3+json',
],
]);
if (is_wp_error($response)) {
return $transient;
}
$releaseData = json_decode(wp_remote_retrieve_body($response), true);
}
if (!empty($releaseData) && !isset($releaseData['message'])) {
// Cache the response in a file
$time = time();
$cacheFile = plugin_dir_path(__FILE__) . $cacheFilename;
file_put_contents($cacheFile, json_encode($releaseData));
$cacheCreatedFile = plugin_dir_path(__FILE__) . $cacheCreatedFilename;
file_put_contents($cacheCreatedFile, $time);
$currentVersion = $transient->checked["$wpPluginSlug/$wpPluginSlug.php"];
$latestVersion = ltrim($releaseData[0]['tag_name'], 'v');
if (version_compare($currentVersion, $latestVersion, '<')) {
// Find asset, which name contains 'wordpress'
foreach ($releaseData[0]['assets'] as $key => $asset) {
if (strpos($asset['name'], 'wordpress') !== false) {
$wpAssetKey = $key;
break;
}
}
if ($wpAssetKey === false) {
return $transient;
}
$wpAsset = $releaseData[0]['assets'][$wpAssetKey];
$transient->response["$wpPluginSlug/$wpPluginSlug.php"] = (object) [
'slug' => $wpPluginSlug,
'new_version' => $latestVersion,
'package' => $wpAsset['browser_download_url'],
'url' => $githubRepoUrl,
];
}
}
return $transient;
});
<?php
/**
* Retrieve updates for a WordPress theme from GitHub.
* Add this snippet to your theme's functions.php file.
* Note: The public GitHub API has a rate limit of 60 requests per hour per IP.
*/
add_filter('site_transient_update_themes', function($transient) {
// Config
$wpThemeSlug = 'your-theme-slug'; // Change this to your theme slug
$githubRepoSlug = 'your-github-username/your-repo-name'; // Change this to your GitHub repo slug
$githubRepoUrl = "https://github.com/$githubRepoSlug";
$githubReleasesUrl = "https://api.github.com/repos/$githubRepoSlug/releases";
$cacheFilename = 'github-releases.json';
$cacheCreatedFilename = 'github-checked-at.txt';
if (empty($transient->checked)) {
return $transient;
}
// Check if the cache file exists
$cacheFile = get_theme_file_path($cacheFilename);
$cacheCreatedFile = get_theme_file_path($cacheCreatedFilename);
// Check if cache is older than 1 hour
$isCacheOld = false;
if (file_exists($cacheCreatedFile)) {
$cacheCreated = file_get_contents($cacheCreatedFile);
$cacheCreatedTimestamp = (int)$cacheCreated;
$currentTimestamp = time();
$diff = $currentTimestamp - $cacheCreatedTimestamp;
if ($diff > 3600) { // Cache is old if more than 1 hour
$isCacheOld = true;
}
}
// Use cached data if available and not old
if (file_exists($cacheFile) && !$isCacheOld) {
$releaseData = json_decode(file_get_contents($cacheFile), true);
} else {
// Fetch the release data from GitHub
$response = wp_remote_get($githubReleasesUrl, [
'timeout' => 10,
'headers' => [
'Accept' => 'application/vnd.github.v3+json',
],
]);
if (is_wp_error($response)) {
return $transient;
}
$releaseData = json_decode(wp_remote_retrieve_body($response), true);
}
if (!empty($releaseData) && !isset($releaseData['message'])) {
// Cache the response
file_put_contents($cacheFile, json_encode($releaseData));
file_put_contents($cacheCreatedFile, time());
// Get current theme version
$theme_data = wp_get_theme($wpThemeSlug);
$currentVersion = $theme_data->Version;
$latestVersion = ltrim($releaseData[0]['tag_name'], 'v');
if (version_compare($currentVersion, $latestVersion, '<')) {
// Find asset in the release data
$wpAssetKey = false;
foreach ($releaseData[0]['assets'] as $key => $asset) {
if (strpos($asset['name'], 'wordpress') !== false) {
$wpAssetKey = $key;
break;
}
}
if ($wpAssetKey === false) {
return $transient;
}
$wpAsset = $releaseData[0]['assets'][$wpAssetKey];
$transient->response[$wpThemeSlug] = [
'theme' => $wpThemeSlug,
'new_version' => $latestVersion,
'url' => $githubRepoUrl,
'package' => $wpAsset['browser_download_url']
];
}
}
return $transient;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment