/** * Return the absolut path to the root of the plugin, or null * if this plugin is a single file. * * @return string|null */ protected function getPath() { if ($this->getPluginFolder() === null) { return null; } else { return WPRequire::PLUGINS_DIR() . "/" . $this->getPluginFolder(); } }
/** * Construct a new WPTheme object * * @param string $path The path to the root folder of the theme */ public function __construct($path) { $this->path = WPRequire::THEMES_DIR() . "/" . $path; $themeObject = wp_get_theme($this->path . "/style.css", $this->path); // Extract the theme data into an assosiativ array $this->themeData = array("Name" => $themeObject->get("Name"), "ThemeURI" => $themeObject->get("ThemeURI"), "Description" => $themeObject->get("Description"), "Author" => $themeObject->get("Author"), "AuthorURI" => $themeObject->get("AuthorURI"), "Version" => $themeObject->get("Version"), "Template" => $themeObject->get("Template"), "Status" => $themeObject->get("Status"), "Tags" => $themeObject->get("Tags"), "TextDomain" => $themeObject->get("TextDomain"), "DomainPath" => $themeObject->get("DomainPath")); if (isset($this->themeData['version'])) { $this->version = new Version($this->themeData['version']); } else { // Null if none is spesified $this->version = null; } parent::__construct(); }
/** * Get active plugins that does not have there requirements met. * returns an array with plugin-base-name=>["this"=>[required-version, supplied-version]] * Supplied version in this array will be "null" if there was non supplied. eg. If it was a plugin * that wa entierly missing. * * @return array plugin-name=>(outdatedOrMissing=>(required, supplied))[] */ private static function getUnsuportedPlugins() { $activePlugins = self::getAllActivePlugins(); $unsuported = []; foreach ($activePlugins as $plugin) { $pluginFile = $plugin->getPluginFile(); $wpRequireFile = $plugin->getWpRequire(); // If no wp-require file exists, assume it has all it needs if ($wpRequireFile === null) { continue; } // Init the $unsuported array for this plugin $unsuported[$pluginFile] = array(); $requiredPhpVersion = $wpRequireFile->getRequiredPhpVersion(); $requiredWpVersion = $wpRequireFile->getRequiredWpVersion(); $requiredPlugins = $wpRequireFile->getRequiredPlugins(); $phpComp = $requiredPhpVersion->isCompatibleWith(self::getPhpVersion()); if (!$phpComp) { $unsuported[$pluginFile]["php"] = array($requiredPhpVersion, self::getPhpVersion()); } $wpComp = $requiredWpVersion->isCompatibleWith(self::getWpVersion()); if (!$wpComp) { $unsuported[$pluginFile]["wp"] = array($requiredWpVersion, self::getWpVersion()); } $unsuported[$pluginFile]['plugins'] = array(); foreach ($requiredPlugins as $requiredPluginFile => $requiredPluginVersion) { if (!self::isPluginActive($requiredPluginFile)) { if (!isset($unsuported[$pluginFile]['plugins'])) { $unsuported[$pluginFile]['plugins'] = array(); } $unsuported[$pluginFile]['plugins'][$requiredPluginFile] = array($requiredPluginVersion, null); } else { $pluginData = get_plugin_data(WPRequire::PLUGINS_DIR() . "/" . $requiredPluginFile); $requiredVersion = new Version($requiredPluginVersion); $suppliedVersion = new Version($pluginData["Version"]); if (!$requiredVersion->isCompatibleWith($suppliedVersion)) { $unsuported[$pluginFile]['plugins'][$requiredPluginFile] = array($requiredPluginVersion, new Version($pluginData["Version"])); } } } // If this plugins plugin requirments was uphelp if (count($unsuported[$pluginFile]['plugins']) === 0) { unset($unsuported[$pluginFile]['plugins']); } // If no reasons for why this plugin is unsuported can be found // Remove it from the array if (count($unsuported[$pluginFile]) === 0) { unset($unsuported[$pluginFile]); } } return $unsuported; }
public function testGetUnsuportedPluginsOutdatedWp() { $WPRequire = new WPRequire(); $mockWpVersionRequire = new Version("10.0.0"); // Create the mock plugin $mockPlugin = WPRequireTestUtils::createMockPlugin(array("wordpress" => (string) $mockWpVersionRequire)); $mockPluginFile = $mockPlugin->getPluginFile(); // Activate the mock plugin WPRequireTestUtils::invokeMethod($WPRequire, "activatePlugin", [$mockPluginFile]); // Get unsuported plugins $unsuported = WPRequireTestUtils::invokeMethod($WPRequire, "getUnsuportedPlugins"); // Check if our mock plugin is considered usuported(as it should) $this->assertTrue(isset($unsuported[$mockPluginFile])); // Test that the PHP reason is marked as expected $this->assertEquals($unsuported[$mockPluginFile]["wp"], array($mockPlugin->getWpRequire()->getRequiredWpVersion(), WPRequire::getWpVersion())); // Test that there is only one reason for this to be unsuported $this->assertEquals(count($unsuported[$mockPluginFile]), 1); }
/** * Create a mock theme that is deleted * when the tests are done running * with a wp-require file * with the contents of $require. * * @param array $requires The desired contents of the wp-require file. * * @return WPTheme The theme */ public static function createMockTheme($requires) { $baseThemesDir = WPRequire::THEMES_DIR(); // Create a random name $themeName = Str::random(20); // Create the plugin directory $themeDir = new File($baseThemesDir . "/{$themeName}"); $themeDir->createDir(); // Create the base plugin file $styleCss = new File($themeDir->getPath() . "/style.css"); $styleCss->createFile(); // Write plugin name and version to the plugin header $styleCssWriter = new FileWriter($styleCss); $styleCssWriter->open(); $styleCssWriter->write("\n/*\n * Theme Name: {$themeName}\n * Version: 1.0.0\n */"); $styleCssWriter->close(); // Create the wp-require.json file $wpRequireFile = new File($themeDir->getPath() . "/wp-require.json"); $wpRequireFile->createFile(); $wpRequireFileWriter = new FileWriter($wpRequireFile); // Write the array as json to the wp-require.json file $wpRequireFileWriter->open(); $wpRequireFileWriter->write((string) new Json($requires)); $wpRequireFileWriter->close(); // Request files to be deleted on tearDown $styleCss->deleteOnExit(); $wpRequireFile->deleteOnExit(); $themeDir->deleteOnExit(); $theme = new WPTheme("{$themeName}"); return $theme; }
<?php /* * Plugin Name: WP Require * Plugin URI: https://github.com/sigurdsvela/wp-require * Description: Handles WordPress plugin and theme requirements. * Version: 0.1.2 * Author: Sigurd Svela * Author URI: https://github.com/sigurdsvela * Text Domain: wp-require * Domain Path: /lang */ // We need some function from here if (is_admin()) { require_once ABSPATH . "/wp-admin/includes/plugin.php"; } // Hot fix. Until phpstd implements composer autoloading. require_once __DIR__ . "/vendor/autoload.php"; require_once __DIR__ . "/core/autoload.php"; use WPRequire\WPRequire; if (!defined('WP_REQUIRE_ABSPATH')) { define('WP_REQUIRE_ABSPATH', __DIR__); } WPRequire::main();