Skip to content
Willington Vega edited this page Nov 24, 2019 · 2 revisions

Welcome to the wc-plugin-framework wiki!

Getting started

Install the framework using composer

Add the following to your composer.json file:

{
  "minimum-stability": "dev",
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/skyverge/wc-plugin-framework"
    }
  ],
  "require" : {
    "skyverge/wc-plugin-framework": "5.5.1"
  }
}

Then run composer to install the framework as a dependency:

composer install

The main plugin file

Copy the example plugin loader class to the plugin's main file. This file uses PHP 5.2 compatible code to check the environment (PHP, WordPress, and WooCommerce versions) before loading the framework classes and initializing your plugin.

Make sure to update the example plugin loader class to:

  1. Replace lib/skyverge with vendor/skyverge/wc-plugin-framework
  2. Update the authors
  3. Complete all the TODO items

The main plugin class

Using PSR4

Update the init_plugin method in the plugin loader class to replace the commented out section with the following:

// autoload plugin and vendor files
$loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' );

// register plugin namespace with autoloader
$loader->addPsr4( 'SkyVerge\\WooCommerce\\Plugin_Name\\', __DIR__ . '/includes' ); // TODO: plugin namespace here

// load the file that contains the initial plugin function
require_once( plugin_dir_path( __FILE__ ) . 'includes/Functions.php' );

Then, create a Plugin class in includes/Plugin.php that extends the SV_WC_Plugin class from the framework:

<?php
/**
 * WooCommerce Framework Plugin
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2014-2019, SkyVerge, Inc.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

namespace SkyVerge\WooCommerce\Framework_Plugin;

defined( 'ABSPATH' ) or exit;

use SkyVerge\WooCommerce\PluginFramework\v5_5_1 as Framework;

/**
 * @since 1.0.0
 */
class Plugin extends Framework\SV_WC_Plugin {


	/** @var Plugin */
	protected static $instance;


	/**
	 * Gets the main instance of Framework Plugin instance.
	 *
	 * Ensures only one instance is/can be loaded.
	 *
	 * @since 1.0.0
	 *
	 * @return Plugin
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


}

Also, add a function to instantiate the main plugin class in includes/Functions.php:

<?php
/**
 * WooCommerce Framework Plugin
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2014-2019, SkyVerge, Inc.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

use SkyVerge\WooCommerce\Framework_Plugin\Plugin;

/**
 * @since 1.0.0
 */
function wc_framework_plugin() {

	return Plugin::instance();
}

Using WordPress class-class-name.php structure

Update the init_plugin method in the plugin loader class to replace the commented out section with the following:

// load the main plugin class
require_once( plugin_dir_path( __FILE__ ) . 'class-wc-framework-plugin.php' ); // TODO: main plugin class file

Then, create the main plugin class in class-wc-framework-plugin.php (change the name of the class to reflect the name of your plugin):

<?php
/**
 * WooCommerce Framework Plugin
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2014-2019, SkyVerge, Inc.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

defined( 'ABSPATH' ) or exit;

use SkyVerge\WooCommerce\PluginFramework\v5_5_1 as Framework;

/**
 * @since 1.0.0
 */
class WC_Framework_Plugin extends Framework\SV_WC_Plugin {


	/** @var \WC_Framework_Plugin */
	protected static $instance;


	/**
	 * Gets the main instance of Framework Plugin instance.
	 *
	 * Ensures only one instance is/can be loaded.
	 *
	 * @since 1.0.0
	 *
	 * @return \WC_Framework_Plugin
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


}

Then, append the following code to the bottom of the main plugin class file:

/**
 * @since 1.0.0
 */
function wc_framework_plugin() {

	return WC_Framework_Plugin::instance();
}

Add a constructor

If you try to activate the plugin as it is, it wil thrown several errors.

First, the constructor for the SV_WC_Plugin class has two required parameters: the plugin version and the plugin ID. We need provide a constructor for our plugin that takes no parameter but still defines the required parameters for the constructor in the parent class.

Add the following constants at the top of the main plugin class file and define the constructor as shown below:

/** plugin version number */
const VERSION = '1.0.0';

/** plugin id */
const PLUGIN_ID = 'framework-plugin';
/**
 * Constructs the plugin.
 *
 * @since 1.0
 */
public function __construct() {

	parent::__construct(
		self::PLUGIN_ID,
		self::VERSION,
		array(
			'text_domain' => 'woocommerce-framework-plugin',
		)
	);
}

Implement abstract methods

The SV_WC_Plugin class also defines two abstract methods that must be implemented in the main plugin class:

/**
 * Gets the full path and filename of the plugin file.
 *
 * @since 1.0.0
 *
 * @return string the full path and filename of the plugin file
 */
protected function get_file() {

    return __FILE__;
}


/**
 * Gets the plugin full name including "WooCommerce", ie "WooCommerce X".
 *
 * @since 1.0.0
 *
 * @return string plugin name
 */
public function get_plugin_name() {

    return __( 'WooCommerce Framework Plugin', 'wc-plugin-framework' ); // TODO: use the plugin name here
}

You should now be able to activate a plugin that uses the WooCommerce Plugin Framework.