When you are developing multiple plugins, you might develop a library that will be used in some of them. So, what happens when your customers might use more than 1 of your plugins with that libary.

How are you going to handle the loading of it so you’re sure that the latest is being used?

In one of the previous tutorials, you could read about Using Composer for Libraries in WordPress Plugins or Themes.

This can be a way to load your libraries but you can’t control the loading order and which version of the library will load.

By only using the autoload.php provided by composer, if your oldest library is loaded first, all your plugins will use that one. That might not be ideal.

So let’s now see how we can load libraries so all your plugins use the latest one.

Creating the Integration Class

Create a Library Integration class which, for the purpose of this tutorial, I’ll call Library_Integration.

<?php

class Library_Integration {
  
  /**
   * Library Version
   * 
   * @var string
   */
  protected $version = '1.0.0';

  /**
   * Library Name
   * 
   * @var string
   */
  protected $name = 'my_library';

}

The $version will define the library version that is included with your WordPress plugin. The $name is the id of your library which we can use to load it.

<?php

class Library_Integration {

  // previous code here ...
  
  /**
   * Register Library Version on the $GLOBALS
   */
  public function register_version() {
    if ( ! isset( $GLOBALS ) ) {
      $GLOBALS = array();
    }

    if ( isset( $GLOBALS ) ) {
      return;
    }
    
    $GLOBALS = $this->get_path();
  }

}

In the method register_version we will register our version inside of the $_GLOBALS (you can change this with a different way of registering them if you need). If the library version used by this plugin is already registered, we won’t register it again.

What are we actually doing is that we are mapping the path to the library with the version.

<?php

class Library_Integration {

  // previous code here ...
  
  /**
   * Get the path to the library
   */
  public function get_path() {
    return 'path/to/main/library.php';
  }

}

Inside of the get_path method, you will set the complete path to your library file. For example, if your library is loaded through composer, you could:

  1. define the main plugin path inside of your main plugin file using define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__));
  2. then inside of the get_path, set it like: trailingslashit( MY_PLUGIN_PATH ) . 'vendor/your_package/library_repo/LibraryFile.php';

In case you would put your library inside of your other folder such as includes, you could change it to that path.

<?php

class Library_Integration {

  // previous code here ...
  
  /**
   * Load latest Library path
   */
  public static function load_latest_path() {
    // Composer will load it.
    if ( ! isset( $GLOBALS ) ) {
      return;
    }
    
    if ( ! $GLOBALS || ! is_array( $GLOBALS ) ) {
      return;
    }
    
    $latest = '0.0.0';
    foreach ( $GLOBALS as $version => $path ) {
      if ( version_compare( $latest, $version, '<' ) ) {
        $latest = $version;
      }
    }

    if ( isset( $GLOBALS ) &amp;&amp; file_exists( $GLOBALS ) ) {
      include_once $GLOBALS;
    }
  }

}

The method load_latest_path will be used to load the latest registered library. This should be used after all plugins have been loaded so you’re sure that all the libraries have been loaded.

The last thing in our integration class, we will need to register the version on instantiating the class.

<?php

class Library_Integration {

  // previous code here ...
  
  /**
   * Adding some support for Condition Engine such as filtering hooks for specific Conditions.
   */
  public function __construct() {
    $this->register_version();
  }
}

Loading your Library in a Plugin

Let’s now see how we can load our library. This is just a simple example and you can change when and where to load it depending on your library and plugin.

<?php

// Somewhere in your plugin
include_once 'class-library-integration.php'
// We registered it.
$library_integration = new Library_Integration();

add_action( 'plugins_loaded', array( $this, 'load_library' );

function load_library() {
  if ( ! class_exists('Library') ) {
    Library_Integration::load_latest_path();
  }
  // this is your framework. Make sure you will call this after you loaded the latest path.
  new Library();
}

Conclusion

By defining how your libraries will load you can make sure that all your plugins, that depend on it, will load the latest library.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.