assets
11 months ago
src
11 months ago
templates
11 months ago
autoload.php
11 months ago
index.php
11 months ago
main.php
11 months ago
sdk_resolver.php
11 months ago
version.php
11 months ago
sdk_resolver.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | if (!function_exists('analyst_resolve_sdk')) { |
| 4 | |
| 5 | /** |
| 6 | * Resolve supported sdk versions and load latest supported one |
| 7 | * also bootstrap sdk with autoloader |
| 8 | * |
| 9 | * @since 1.1.3 |
| 10 | * |
| 11 | * @param null $thisPluginPath |
| 12 | * @return void |
| 13 | * @throws Exception |
| 14 | */ |
| 15 | function analyst_resolve_sdk($thisPluginPath = null) { |
| 16 | static $loaded = false; |
| 17 | |
| 18 | // Exit if we already resolved SDK |
| 19 | if ($loaded) return; |
| 20 | |
| 21 | $plugins = get_option('active_plugins'); |
| 22 | |
| 23 | if ($thisPluginPath) { |
| 24 | array_push($plugins, plugin_basename($thisPluginPath)); |
| 25 | } |
| 26 | |
| 27 | $pluginsFolder = WP_PLUGIN_DIR; |
| 28 | |
| 29 | $possibleSDKs = array_map(function ($path) use ($pluginsFolder) { |
| 30 | $sdkFolder = sprintf('%s/%s/analyst/', $pluginsFolder, dirname($path)); |
| 31 | |
| 32 | $sdkFolder = str_replace('\\', '/', $sdkFolder); |
| 33 | |
| 34 | $versionPath = $sdkFolder . 'version.php'; |
| 35 | |
| 36 | if (file_exists($versionPath)) { |
| 37 | return require $versionPath; |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | }, $plugins); |
| 42 | |
| 43 | global $wp_version; |
| 44 | |
| 45 | // Filter out plugins which has no SDK |
| 46 | $SDKs = array_filter($possibleSDKs, function ($s) {return is_array($s);}); |
| 47 | |
| 48 | // Filter SDKs which is supported by PHP and WP |
| 49 | $supported = array_values(array_filter($SDKs, function ($sdk) use($wp_version) { |
| 50 | $phpSupported = version_compare(PHP_VERSION, $sdk['php']) >= 0; |
| 51 | $wpSupported = version_compare($wp_version, $sdk['wp']) >= 0; |
| 52 | |
| 53 | return $phpSupported && $wpSupported; |
| 54 | })); |
| 55 | |
| 56 | // Sort SDK by version in descending order |
| 57 | uasort($supported, function ($x, $y) { |
| 58 | return version_compare($y['sdk'], $x['sdk']); |
| 59 | }); |
| 60 | |
| 61 | // Reset sorted values keys |
| 62 | $supported = array_values($supported); |
| 63 | |
| 64 | if (!isset($supported[0])) { |
| 65 | throw new Exception('There is no SDK which is support current PHP version and WP version'); |
| 66 | } |
| 67 | |
| 68 | // Autoload files for supported SDK |
| 69 | $autoloaderPath = str_replace( |
| 70 | '\\', |
| 71 | '/', |
| 72 | sprintf('%s/autoload.php', $supported[0]['path']) |
| 73 | ); |
| 74 | |
| 75 | require_once $autoloaderPath; |
| 76 | |
| 77 | $loaded = true; |
| 78 | } |
| 79 | } |
| 80 |