Closed Urano-Gonzalez closed 4 years ago
As long as all of the plugins are using a reasonably recent version of the library, it should be fine to have multiple plugins where each has its own copy of PUC. The versions don't have to be the same, just recent enough to avoid some known bugs in older versions that could cause conflicts.
You could also have one plugin load the library and let other plugins use it, or have one plugin that updates all of the other plugins. You would need to build the latter yourself since PUC doesn't have any built-in support for that.
Thanks @YahnisElsts for your hint with updating all plugins from within one plugin! That brought me to this solution:
/**
* Plugin Name: RH Updater
* Version: 1.1.0
* Author: Rasso Hilber
* Description: Manage custom plugin updates
* Author URI: https://rassohilber.com
**/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
require_once(__DIR__ . '/vendor/autoload.php');
class RH_Bitbucket_Updater {
private $registered_plugins = [];
/**
* Register a plugin for updates
*
* @param string $file
* @return boolean Success status
*/
public function register_plugin( $file_path ) {
// return early if the plugin doesn't exist
if( !file_exists($file_path) ) return false;
// return early the plugin was already registered
if( in_array($file_path, $this->registered_plugins) ) return false;
$this->registered_plugins[] = $file_path;
$plugin_slug = basename( dirname( $file_path ) );
$update_checker = \Puc_v4_Factory::buildUpdateChecker(
"https://bitbucket.org/rhilber/$plugin_slug", // Metadata URL.
$file_path, // Full path to the main plugin file.
$plugin_slug, // Plugin slug. Usually it's the same as the name of the directory.
24*6 // <-- update interval in hours
);
$update_checker->setAuthentication(array(
'consumer_key' => 'XXX',
'consumer_secret' => 'XXX',
));
return true;
}
}
// The slugs of my custom plugins (`my-plugin-folder/my-plugin-file.php`)
$rh_updater_config = [
'bitbucket' => [
'rh-updater/rh-updater.php',
'rh-free-layouts/rh-free-layouts.php',
'rh-privacy-consent/rh-privacy-consent.php',
'rh-admin-utils/rh-admin-utils.php',
'rh-editors-add-users/rh-editors-add-users.php',
'rh-environments/rh-environments.php',
'rh-shortcodes/rh-shortcodes.php',
'rh-wpsc-clear-cache/rh-wpsc-clear-cache.php',
'rh-privacy-settings/rh-privacy-settings.php',
'rh-acf-frontend-forms/rh-acf-frontend-forms.php',
'rh-l10n/rh-l10n.php',
],
];
// The Updater Class
$bb_updater = new RH_Bitbucket_Updater();
// traverse through all plugins, and if they exist, register them for updates.
foreach( $rh_updater_config['bitbucket'] as $file ) {
$file = trailingslashit(WP_PLUGIN_DIR) . $file;
$bb_updater->register_plugin( $file );
}
None of the other plugins need any extra code anymore. Everything is managed from this plugin. If I create a new plugin, I just add it to my $rh_updater_config
array. Yay! 😃
Thanks @hirasso How appears in dashboard the links for update? Can you share a image, please?
Hi @Urano-Gonzalez , it looks just like the default WordPress plugin updates. PUC just takes care of checking for updates, everything else is WP core behavior. Here's a screenshot of my /wp-admin/plugins.php
screen:
Thanks a lot!
Please close this issue!! thanks a lot to @YahnisElsts and @hirasso
Hi. Great library! Is awesome how my customer can update the plugin from his site. Question: If one site have 2 o more of my plugins, the library in inside each plugin. There are any possible conflicts? Is posible share the library among all plugins?