.. _extending_create_a_module: ###################### Create a module ###################### This chapter will show you how to create a custom module for Post Status Notifier. ************ Requirements ************ A module must consist of at least a **directory** containing a **bootstrap.php** file. The directory is mandatory, a single bootstrap.php file will not be accepted. The module directory must only contain alphanumerical characters, no whitespace or special characters. This is the structure of a minimalistic module setup, a directory called "Example" containing a file named "bootstrap.php". .. code:: jinja Example └ bootstrap.php Module package ~~~~~~~~~~~~~~ The final module must be **packaged in a zip file** to be able to upload it through the Module Manager. It must contain the folder, in this case "Example", on the root level. .. image:: _static/extend_archive_example.jpg :height: 176px :width: 323px :scale: 100% :alt: Module archive .. _module_bootstrap: ******************** The module bootstrap ******************** Naming conventions ~~~~~~~~~~~~~~~~~~ The module's bootstrap.php file must contain a PHP class. This class must adhere to naming conventions. For example, if your module directory is called **"Example"**, your bootstrap class must be called **"Example_Bootstrap"**. If your module directory is called **"MyAwesomeModule"**, your bootstrap class must be called **"MyAwesomeModule_Bootstrap"** and so on. Additionally the bootstrap class must **extend "IfwPsn_Wp_Module_Bootstrap_Abstract"**. .. code:: _pm->getInstaller(); It features three methods to add your own logic: addActivation ============= The ``addActivation`` method gets called when PSN gets activated in the WordPress plugin backend. It expects an object implementing the interface ``IfwPsn_Wp_Plugin_Installer_ActivationInterface``. .. code:: // e.g. in your bootstrap file $this->_pm->getInstaller()->addActivation( new Psn_Module_Example_Installer_Activation() ); Objects implementing the interface ``IfwPsn_Wp_Plugin_Installer_ActivationInterface`` must implement the **public method** ``execute``. There you can place your custom activation code like in this example: .. code:: class Psn_Module_Example_Installer_Activation implements IfwPsn_Wp_Plugin_Installer_ActivationInterface { /** * @param IfwPsn_Wp_Plugin_Manager $pm * @param bool $networkwide - true if WordPress multisite is activated * @return mixed|void */ public function execute( IfwPsn_Wp_Plugin_Manager $pm, $networkwide = false ) { // here you could for example create a new database table } } addDeactivation =============== The ``addDeactivation`` method gets called when PSN gets deactivated in the WordPress plugin backend. It expects an object implementing the interface ``Ifw_Wp_Plugin_Installer_DeactivationInterface``. .. code:: // e.g. in your bootstrap file $this->_pm->getInstaller()->addDeactivation( new Psn_Module_Example_Installer_Deactivation() ); Objects implementing the interface ``IfwPsn_Wp_Plugin_Installer_DeactivationInterface`` must implement the **public method** ``execute``. There you can place your custom deactivation code like in this example: .. code:: class Psn_Module_Example_Installer_Deactivation implements IfwPsn_Wp_Plugin_Installer_DeactivationInterface { /** * @param IfwPsn_Wp_Plugin_Manager $pm * @param bool $networkwide - true if WordPress multisite is activated * @return mixed|void */ public function execute( IfwPsn_Wp_Plugin_Manager $pm, $networkwide = false ) { // if you want to execute code when the plugin gets deactivated, place it here } } addUninstall ============ The ``addUninstall`` method gets called when PSN gets deleted in the WordPress plugin backend. It expects an object implementing the interface ``Ifw_Wp_Plugin_Installer_UninstallInterface``. .. code:: // e.g. in your bootstrap file $this->_pm->getInstaller()->addUninstall( new Psn_Module_Example_Installer_Uninstall() ); Objects implementing the interface ``IfwPsn_Wp_Plugin_Installer_UninstallInterface`` must implement the **public static method** ``execute``. There you can place your custom uninstall code like in this example: .. code:: class Psn_Module_Example_Installer_Uninstall implements IfwPsn_Wp_Plugin_Installer_UninstallInterface { /** * @param IfwPsn_Wp_Plugin_Manager|null $pm * @return mixed|void */ public static function execute($pm) { // for example remove database tables if the plugin gets deleted if (IfwPsn_Wp_Proxy_Blog::isMultisite()) { // on multisite installation } else { // on single blog installation } } }