Skip to content
Dan Ruscoe edited this page Dec 7, 2018 · 18 revisions

When migrating a plugin that implements this framework from v4.9 or earlier to v5, the biggest change will be handling the new namespaces. Additionally, there are a few backwards-incompatible changes that will need to be handled. The following steps should cover most of the changes required, though it's always important to test a plugin's full range of functionality after the migration.

Composer

  1. Delete the plugin's /lib/skyverge directory and commit
  2. Add a new composer.json file to the plugin or project root, containing:
{
  "minimum-stability": "dev",
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/skyverge/wc-plugin-framework"
    }
  ],
  "require" : {
    "skyverge/wc-plugin-framework": "5.3.0"
  }
}
  1. Change the package version to the desired tagged framework version, or if working off a specific branch, use dev- then the branch name, e.g. "skyverge/wc-plugin-framework": "dev-5.2.0-dev"
  2. Commit and run composer install

Loader Class

Plugins will now require a loader class to handle unsupported PHP versions. The loader class must be defined and instantiated in the main plugin file, and is meant to halt any further plugin code from being included if the environment can't support it. To implement the loader class in an existing plugin:

  1. Create a new file in the plugin root that will contain the main plugin class and name it according to WordPress standards. For instance, if the plugin class (which should extend SV_WC_Plugin) is WC_Authorize_Net_AIM, then the new file would be /class-wc-authorize-net-aim.php
  2. Cut the entire plugin class from the main plugin file into this new class file, also include the main plugin function definition, which usually follows the class. Do not copy the init_ wrapping function if one exists.
  3. Search for a usage of the legacy sv_wc_framework_plugins_loaded bootstrap action, usually within the main plugin class constructor, and instead call its callback directly. e.g. add_action( 'sv_wc_framework_plugins_loaded', array( $this, 'includes' ) ); becomes just $this->includes();.
  4. From the main plugin file, clear any code after the is_woocommerce_active() check.
  5. Replace with the contents of the framework example loader class, and clear all of the TODO items for renaming classes and methods where necessary.

Namespaces

  1. On every PHP plugin file except for the main file, add a use statement directly under the ABSPATH check for the framework's namespaces: use SkyVerge\WooCommerce\PluginFramework\v5_1_5 as Framework;Be sure to replace the version namespace with the framework version you're implementing.
  2. Add the Framework\ namespace alias to every use of a framework class. Often you can simply search & replace: SV_WC_ > Framework\SV_WC_.
  3. Review the changes to ensure you've caught all of the instances, and that the new alias is used correctly in all cases.

Constructor Args

  1. Remove display_php_notice
  2. Update the dependencies array to match what's expected by SV_WC_Plugin::init_dependencies()

Includes

Some plugins will handle their includes and handler loading differently, so you'll have to handle each on a case-by-case basis. Some general notes:

  • We've added the SV_WC_Plugin::init_plugin() & SV_WC_Plugin::init_admin() methods that are fired at init and admin_init respectively, and can be overridden for easy plugin setup. These are not required, but may be helpful.
  • It's generally best practice to instantiate handlers and other classes that get assigned to plugin vars on init, rather than in an includes() method from the plugin constructor. ->load_class() calls can be moved to SV_WC_Plugin::init_plugin() if init is early enough. Be sure to test each one!

Paths

Check if there is plugin code (PHP, static assets like SCSS) referencing directly files or other assets, including framework files, that are expected to be found in the /vendor path or /lib path. These paths may need to be updated after installing the framework and other libraries via composer.

Check for paths matching /lib/skyverge/woocommerce/assets/.

Lifecycle

As of v5.2.0, the plugin lifecycle methods have been moved to SkyVerge\WooCommerce\PluginFramework\v5_2_0\Plugin\Lifecycle. If the plugin has existing upgrade or install routines, they will need to be moved to a class that extends this.

  1. Create a class that extends SkyVerge\WooCommerce\PluginFramework\v5_2_0\Plugin\Lifecycle
  2. Copy the upgrade() && install() methods from the main plugin into this new class and adapt them as needed. The FW class has a get_plugin() method, so instances of $this-> referencing the plugin can be changed to $this->get_plugin()->
  3. Important -- Be sure to fully test the execution of each upgrade and install path after migration

Integrations

  1. Add the following methods to any class that implements Framework\SV_WC_API_Request or extends Framework\SV_WC_API_XML_Request:

Gateways

  1. Add the get_payment_type() method to any response class that implements Framework\SV_WC_Payment_Gateway_API_Response, returning the appropriate payment type.
  2. If overridden, ensure the gateway's do_credit_card_capture() method matches the new signature, and handles the new return value (array).
  3. If overridden, sure the gateway's get_order_for_capture() method matches the new signature.
  4. Search for any hardcoded card type strings, such as mc or diners, and switch them to using the gateway helper constants. Be sure to test these changes thoroughly as some gateways can return different card type strings.

That's it! The plugin should now be ready to fully test.

Deprecated Methods

  • SV_WC_Plugin::do_install()
  • SV_WC_Plugin::install_default_settings() -> Plugin\Lifecycle::install_default_settings()
  • SV_WC_Plugin::get_missing_extension_dependencies() -> SV_WC_Plugin_Dependencies::get_missing_php_extensions()
  • SV_WC_Plugin::get_missing_function_dependencies() -> SV_WC_Plugin_Dependencies::get_missing_php_functions()
  • SV_WC_Plugin::get_incompatible_php_settings() -> SV_WC_Plugin_Dependencies::get_incompatible_php_settings()
  • SV_WC_Plugin::get_dependencies()
  • SV_WC_Plugin::get_extension_dependencies() -> SV_WC_Plugin_Dependencies::get_php_extensions()
  • SV_WC_Plugin::get_function_dependencies() -> SV_WC_Plugin_Dependencies::get_php_functions()
  • SV_WC_Plugin::get_php_settings_dependencies() -> SV_WC_Plugin_Dependencies::get_php_settings()
  • SV_WC_Plugin::set_dependencies()