Reading list Switch to dark mode

    How to Use Submodules in WooCommerce Addons?

    Updated 16 February 2024

    For using submodules in WooCommerce addons we need to configure it using GitHub submodule commands. It helps in modularizing our and re-usability in different modules helps in easily maintainable code.

    We have implemented submodules in our different add-ons like WooCommerce PoS and WooCommerce Wallet System and working for the same with other addons also.

    What is Submodule?

    A submodule is an easy way of maintaining some code block, pattern, or feature bundle that can be written once and can be of use in multiple modules with the latest changes.

    Submodule Commands

    Let’s catch up on some important commands for configuring and updating submodules.

    1. git submodule add

    Using this command we can add an existing submodule to a main module. the syntax for this command is like git submodule add {git-url-to-submodule}

    Searching for an experienced
    Woocommerce Company ?
    Find out More

    2. git submodule update –init –recursive

    This command is useful when we have multiple submodules implemented in a main module and we need to update them all also initialize them if any of them are initialized earlier.

    Use Case

    In our earlier series of blogs, we explained using caching in WooCommerce addons and using Redis for API caching in WooCommerce.

    Now since caching is a common feature, it can be of use in any module to cache the data obtained from the database.

    We have implemented the submodule to use the latest caching technique in each module.

    We have created a submodule repo as ‘wk_caching‘ and added this in one of our Popular Plugin Multi Vendor Marketplace for WooCommerce

    using-submodule-in-woocommerce-plugins

    In the above image, we have cloned our marketplace module then we have added our wk_caching submodule using the git submodule add command.

    After that, it will check the master branch of the submodule wk_caching.

    Implementations of submodule

    Now to use the caching methods for storing and retrieving our data from the cache, we’ll add a core loader module class class-wk-caching-core-loader.php

    <?php
    /**
     * WK Caching Core loader class.
     *
     * @package Multi Vendor Marketplace
     */
    defined( 'ABSPATH' ) || exit(); // Exit if access directly.
    
    /**
     * This file is to initiate core and to run some common methods and decide which WK_Caching_Loader core should run.
     */
    if ( ! class_exists( 'WK_Caching_Core_Loader' ) ) {
    	/**
    	 * WK_Caching_Core_Loader class.
    	 */
    	class WK_Caching_Core_Loader {
    		/**
    		 * Plugins array.
    		 *
    		 * @var @plugins
    		 */
    		public static $plugins = array();
    
    		/**
    		 * Loaded.
    		 *
    		 * @var $loaded.
    		 */
    		public static $loaded = false;
    
    		/**
    		 * Version
    		 *
    		 * @var $version.
    		 */
    		public static $version = null;
    
    		/**
    		 * Include core.
    		 *
    		 * @return void
    		 */
    		public static function include_core() {
    			$get_configuration = self::get_the_latest();
    
    			if ( false === self::$loaded && $get_configuration && is_array( $get_configuration ) && isset( $get_configuration['class'] ) ) {
    				if ( is_callable( array( $get_configuration['class'], 'load_files' ) ) ) {
    					self::$version = $get_configuration['version'];
    					self::$loaded  = true;
    					call_user_func( array( $get_configuration['class'], 'load_files' ) );
    				}
    			}
    		}
    
    		/**
    		 * Register.
    		 *
    		 * @param array $configuration Configuration.
    		 *
    		 * @return void
    		 */
    		public static function register( $configuration ) {
    			array_push( self::$plugins, $configuration );
    		}
    
    		/**
    		 * Get the latest.
    		 *
    		 * @return array
    		 */
    		public static function get_the_latest() {
    			$get_all = self::$plugins;
    			uasort(
    				$get_all,
    				function ( $a, $b ) {
    					if ( version_compare( $a['version'], $b['version'], '=' ) ) {
    						return 0;
    					} else {
    						return ( version_compare( $a['version'], $b['version'], '<' ) ) ? - 1 : 1;
    					}
    				}
    			);
    
    			$get_most_recent_configuration = end( $get_all );
    
    			return $get_most_recent_configuration;
    		}
    	}
    }
    
    
    if ( ! class_exists( 'WKMP_WK_Caching_Core' ) ) {
    	/**
    	 * WKMP_WK_Caching_Core
    	 */
    	class WKMP_WK_Caching_Core {
    		/**
    		 * WK Caching Loading Version.
    		 *
    		 * @var $version
    		 */
    		public static $version = WKMP_LITE_WK_CACHING_VERSION;
    
    		/**
    		 * Register.
    		 *
    		 * @return void
    		 */
    		public static function register() {
    			$configuration = array(
    				'basename'    => WKMP_LITE_PLUGIN_BASENAME,
    				'version'     => self::$version,
    				'plugin_path' => WKMP_LITE_PLUGIN_FILE,
    				'class'       => __CLASS__,
    			);
    			WK_Caching_Core_Loader::register( $configuration );
    		}
    
    		/**
    		 * Load files.
    		 *
    		 * @return bool
    		 */
    		public static function load_files() {
    			$get_global_path = WKMP_LITE_PLUGIN_FILE . 'wk_caching/';
    
    			if ( false === @file_exists( $get_global_path . '/includes/class-wk-caching.php' ) ) {
    				_doing_it_wrong( __FUNCTION__, esc_html__( 'WK Caching Core should be present in folder \'wk_caching\includes\' in order to run this properly.', 'wk-marketplace' ), esc_html( self::$version ) );
    				if ( defined( 'WKWC_DEV' ) && true === WKWC_DEV ) {
    					die( 0 );
    				}
    				return false;
    			}
    
    			/**
    			 * Loading Core caching Files.
    			 */
    			require_once $get_global_path . 'includes/class-wk-caching.php';
    
    			if ( WK_CACHING_VERSION === self::$version ) {
    				do_action( 'wk_caching_loaded', $get_global_path );
    			} else {
    				_doing_it_wrong( __FUNCTION__, esc_html__( 'WK Caching Core should be at the same version as declared in your "class-wk-caching-core-loader.php"', 'wk-marketplace' ), esc_html( self::$version ) );
    				if ( defined( 'WKWC_DEV' ) && true === WKWC_DEV ) {
    					die( 0 );
    				}
    			}
    		}
    	}
    	WKMP_WK_Caching_Core::register();
    }

    We’ll add this file in each module where we need to implement the caching submodule. It will ensure to load only the latest version of the submodule folder from only one module.

    Invoking the Core

    invoking-submodule-in-addon

    Now after including the submodule core, we need to invoke it, and we have to include the caching core loader file and then call the ‘include_core’ function on the plugins_loaded hook..

    Conclusion

    After this configuration, we can implement all the caching functions used in our earlier blog through the module. So, that’s all about using submodules in WooCommerce plugins.

    For any technical assistance, please reach us by mail at [email protected]

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home