Extending WP Pusher
The core of WP Pusher is built around a dependency container and is therefore very extendible. The container makes it super simple to add new dependencies or replace existing ones. WP Pusher also has a few hooks that you can hook into if you want to build an extension to the plugin.
Hooks
WP Pusher has 4 event hooks that are useful if you are building an extension, such as the WP Pusher Slack extension. The 4 hooks are:
- wppusher_plugin_was_installed
- wppusher_theme_was_installed
- wppusher_plugin_was_updated
- wppusher_theme_was_updated
The plugin hooks return a plugin file object and the theme hooks return a stylesheet object. Here's how one of them is used in the Slack notifications plugin:
<?php | |
// ... | |
add_action('wppusher_theme_was_updated', function($stylesheet) use ($notifier) { | |
$notification = ThemeWasUpdated::fromStylesheet($stylesheet); | |
$notifier->notify($notification); | |
}); |
The container
The WP Pusher container is a typical dependency injection container. It has 2 public methods, bind() to add a new dependency and make() to create a new dependency. When registering a dependency, you should use the "wppusher_register_dependency" hook. As an example, let's say we wanted to build a WP Pusher extension that could install plugins and themes from Dropbox. Here's how that could be done:
<?php | |
// We need a new repository | |
class DropboxRepository extends Repository | |
{ | |
public $code = 'db'; | |
public function getZipUrl() | |
{ | |
// Somehow fetch URL to zipball from Dropbox ... | |
// ... and return it | |
} | |
} | |
// Bind the repository to the container | |
// NOTE: This is actually not neccessary, since WP Pusher can automatically | |
// compose dependecies by using PHP's reflection API. This would only be neccessary | |
// if you wanted to bind DropboxRepository to an interface implementation. | |
add_action('wppusher_register_dependency', function(Pusher $pusher) { | |
$pusher->bind('DropboxRepository', 'DropboxRepository'); | |
}); | |
// Then we need to overwrite the RepositoryFactory (this is just | |
// an example and should probably not be done unless it's a private | |
// plugin, since it could cause conflicts with other similar implementations) | |
// so WP Pusher knows how to create a Dropbox repository: | |
class MyOwnRepositoryFactory | |
{ | |
public function build($type, $handle) | |
{ | |
if ($type === 'db') { | |
return new DropboxRepository($handle); | |
} | |
return parent::build($type, $handle); | |
} | |
} | |
// We can then overwrite the RepositoryFactory in the container, | |
// so that an instance of MyOwnRepositoryFactory is returned | |
// whenever someone needs a repository factory: | |
add_action('wppusher_register_dependency', function(Pusher $pusher) { | |
$pusher->bind('RepositoryFactory', 'MyOwnRepositoryFactory'); | |
}); | |
// Now, you'd only need a few adjustments to the UI, to allow users to | |
// select Dropbox as a repository! |
Note: This is very technical and you definitely don't need to understand any of this to use WP Pusher! :-) Also, if you are building something like this for WP Pusher, you should definitely reach out, so we could discuss and brainstorm it together! 💪
Need any help?
If you have any questions about WP Pusher, Git or WordPress, our email is hi@wppusher.com. Don't hesitate shooting us a message! You can also click the little ❤️ in the corner of this page.