Created
August 9, 2017 10:41
-
-
Save MikhailRoot/fccdf066d883682c335c33ec749a4e31 to your computer and use it in GitHub Desktop.
Example of how to use plugin-update-checker to check for updates multiple plugins and enable automatic background updates if needed.
This file contains hidden or 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 | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
require_once __DIR__. '/plugin-update-checker-4.2/plugin-update-checker.php'; | |
class MyPluginUpdater { | |
private $bitbucketBaseUrl = 'https://bitbucket.org/bitbucket_user_name/'; | |
private $bitbucketConsumerKey = 'key_here'; | |
private $bitbucketConsumerSecret = 'secret_here'; | |
private $stableBranchName = 'master'; | |
private $plugins = []; | |
private $updaters =[]; | |
private $masterPluginFILE = WP_PLUGIN_DIR. DIRECTORY_SEPARATOR . 'masterplugin'. DIRECTORY_SEPARATOR . 'masterplugin.php'; | |
private $masterPluginSlug = 'masterplugin'; | |
public function __construct($enableAutoUpdates = false) { | |
$this->plugins = glob(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'familyprefix-*' . DIRECTORY_SEPARATOR .'familyprefix-*.php'); | |
// master plugin enqueue separately first. | |
$this->updaters[$this->masterPluginSlug] = Puc_v4_Factory::buildUpdateChecker( | |
$this->bitbucketBaseUrl . $this->masterPluginSlug, | |
$this->masterPluginFILE, | |
$this->masterPluginSlug | |
); | |
// lets init checkers for updates for child plugins all child plugins have familyprefix- starting their names with. | |
foreach($this->plugins as $pluginFILE){ | |
$parts = pathinfo($pluginFILE); | |
$pluginSlug = $parts['filename']; | |
$this->updaters[$pluginSlug] = Puc_v4_Factory::buildUpdateChecker( | |
$this->bitbucketBaseUrl . $pluginSlug, | |
$pluginFILE, | |
$pluginSlug | |
); | |
} | |
// lets set all common access settings for updaters | |
foreach($this->updaters as $slug => &$updater){ | |
$updater->setAuthentication(array( | |
'consumer_key' => $this->bitbucketConsumerKey, | |
'consumer_secret' => $this->bitbucketConsumerSecret, | |
)); | |
$updater->setBranch( $this->stableBranchName ); | |
} | |
if( $enableAutoUpdates ){ | |
add_filter( 'auto_update_plugin', [$this, 'enableAutoUpdatesForPlugins'], 10, 2 ); | |
} | |
} | |
public function enableAutoUpdatesForPlugins($update, $item){ | |
if( array_key_exists( $item->slug, $this->updaters ) ){ | |
return true; | |
} | |
return $update; | |
} | |
} | |
// then in your master plugin instantiate this class in a constructor for example and | |
$MyPluginUpdater = new MyPluginUpdater(true); // to enable autoupdates. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment