wp-media/plugin-family

Organizes and displays WP Media plugin family across other members.

v1.0.3 2024-11-28 16:53 UTC

README

This package bundles the WP Media plugin collection within a single plugin.

Functionality

This package gathers essential data about each member plugin and provides their installation and activation links, similar to the Imagify Partner package. It returns an installation link for plugins that are not installed, and an activation link for those that are installed but not active. For Imagify, the link generated by the Imagify Partner package is used.

PS: The Imagify Partner package needs be installed along side. refer here for more information.

Installations & Configurations

Mozart

It is strongly advised that this package is loaded using Mozart. This is to avoid package conflicts where the same package is being used in another plugin and loaded from the same namespace. So please, take the pain to install mozart as a development dependency of your plugin: composer require coenjacobs/mozart --dev

  • After installation, now you are set to configure mozart: see here or simply refer to how it's done on wp-rocket here, there's no shame.
  • Lastly setup (post install & update) scripts as done here

Package

Now for the package, we need to come back to composer.json file again.

  • Remember the extra key we added while setting up mozart some minutes ago, Bingo! Add a new property in it like this: "plugin_domain": "your_text_domain". Replace your_text_domain with your actual text domain. e.g for WP Rocket, it's rocket

  • Now again, remember the (post install & update) script we also created, we'll add new properties there also like this:

    "post-install-cmd": [
        "Mozart\\dep_namespace\\WPMedia\\PluginFamily\\PostInstall::apply_text_domain"
    ],
    "post-update-cmd": [
        "Mozart\\dep_namespace\\WPMedia\\PluginFamily\\PostInstall::apply_text_domain"
    ],

    Replace Mozart\\dep_namespace with the dep_namespace you configured for mozart earlier.

    PS: The example above doesn't not take into consideration the mozart scripts. so don't copy directly and overwrite your scripts.

  • Finally, install using composer: composer require wp-media/plugin-family

Usage Instructions

  • Import the model that holds the filtered data into your view class.
use WPMedia\PluginFamily\Model\PluginFamily;

class View {
    protected $plugin_family;

    public function __construct( $plugin_family ) {
        $this->plugin_family = $plugin_family;
    }

    public function display_page() {
		$plugin_family = $this->plugin_family->get_filtered_plugins( 'imagify/imagify' );

		$data = [
			'plugin_family' => $plugin_family['uncategorized'],
		];

		$this->print_template( 'page-settings', $data );
	}
}

The model returns an array with 2 keys ( categorized & uncategorized ). This for plugins like WP Rocket that needs to display the plugins by category.

  • The categorized version has the plugins grouped by their categories.
  • The uncategorized version is the reverse of the former.

Next, we need to invoke the controller responsible for managing the installation and activation. This controller has a corresponding interface that needs to be implemented.

use WPMedia\PluginFamily\Controller\{ PluginFamily, PluginFamilyInterface };

class Subscriber implements SubscriberInterface, PluginFamilyInterface {

    protected $plugin_family;

    public function __construct( $plugin_family ) {
        $this->plugin_family = $plugin_family;
    }

	public static function get_subscribed_events() {
        $events = PluginFamily::get_subscribed_events();

        return $events;
	}

    public function install_activate() {
        $this->plugin_family->install_activate();
    }

    public function display_error_notice() {
        $this->plugin_family->display_error_notice();
    }
}

The methods ( install_activate & display_error_notice ) are required.

PS: This example is based on the assumption that your plugin utilizes EDA. If EDA is not implemented, you can iterate through the events returned by PluginFamily::get_subscribed_events() and use add_action accordingly.

Development & Testing

To facilitate development and testing of the package, it is recommended to specify a development branch in the composer.json file of your project.

Taking WP Rocket as example

"require": {
    "wp-media/plugin-family": "dev-whatever-dev-branch"
}

PS: Always have the dev prefix before the actual branch.

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/wp-media/plugin-family"
        }
    ]