Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active June 1, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/be5a5660272db9d0dc8c527fac49ad92 to your computer and use it in GitHub Desktop.
Save igorbenic/be5a5660272db9d0dc8c527fac49ad92 to your computer and use it in GitHub Desktop.
Loading Your Library only once between WordPress Plugins | https://www.ibenic.com/loading-library-once-wordpress-plugins/
<?php
class Library_Integration {
// previous code here ...
/**
* Load latest Library path
*/
public static function load_latest_path() {
// Composer will load it.
if ( ! isset( $GLOBALS[ $this->name ] ) ) {
return;
}
if ( ! $GLOBALS[ $this->name ] || ! is_array( $GLOBALS[ $this->name ] ) ) {
return;
}
$latest = '0.0.0';
foreach ( $GLOBALS[ $this->name ] as $version => $path ) {
if ( version_compare( $latest, $version, '<' ) ) {
$latest = $version;
}
}
if ( isset( $GLOBALS[ $this->name ][ $latest ] ) && file_exists( $GLOBALS[ $this->name ][ $latest ] ) ) {
include_once $GLOBALS[ $this->name ][ $latest ];
}
}
}
<?php
class Library_Integration {
// previous code here ...
/**
* Register Library Version on the $GLOBALS
*/
public function register_version() {
if ( ! isset( $GLOBALS[ $this->name ] ) ) {
$GLOBALS[ $this->name ] = array();
}
if ( isset( $GLOBALS[$this->name ][ $this->version ] ) ) {
return;
}
$GLOBALS[ $this->name ][ $this->version ] = $this->get_path();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment