Last active
June 1, 2020 18:33
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ]; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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