eightshift-libs icon indicating copy to clipboard operation
eightshift-libs copied to clipboard

Getting project version and name won't work in the case of a plugin

Open dingo-d opened this issue 2 years ago • 2 comments
trafficstars

So, while the solution for streamlining the project version and name from the wp_get_theme object works great if we have a theme, in the case of a plugin, there could be some issues, as these would return false.

https://github.com/infinum/eightshift-libs/blob/aebc4aef67f5da9b5bcd58384594775fd8589551/src/Config/ConfigExample.php#L30

The solution would be to check if the project is a theme or a plugin (something like, check 3 directories from the config file, and if that is plugins it's a plugin, if it's a themes it's a theme), then either call the above code, or use get_plugin_data() function to get the information about the plugin from the plugin header

POC would be like (inside the Config class):

/**
 * Get data about the plugin
 *
 * @return array Array of plugin data from the plugin headers.
 */
private static function getPluginData(): array
{
	if (!function_exists('get_plugin_data')) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	$pluginName = basename(plugin_dir_path(dirname(__DIR__)));
	return get_plugin_data(dirname(__DIR__, 2) . '/' . $pluginName . '.php');
}

Then, using that helper you'd use it in the getProjectName() method like

$pluginData = self::getPluginData();

$textDomain = $pluginData['TextDomain'];

$textDomain = ucwords(str_replace('-', ' ', $textDomain));
$textDomain = str_replace(' ', '', $textDomain);

return lcfirst($textDomain);

EDIT: the last part would return a camelCased string, instead of the text domain, which is usually a lower cased dash separaed string.

dingo-d avatar Nov 20 '23 09:11 dingo-d

The plan is to provide a different config file for plugins with this part in it

iruzevic avatar Nov 20 '23 09:11 iruzevic

The plan is to provide a different config file for plugins with this part in it

How come? They are pretty much the same, and just adding a check in the same file would reduce code duplication 🤷🏼‍♂️

dingo-d avatar Nov 20 '23 09:11 dingo-d

This should be fixed after new version if I'm not mistaken.

dingo-d avatar Jun 05 '24 08:06 dingo-d

yap

iruzevic avatar Jun 05 '24 08:06 iruzevic

i will update the migration guide today and update the docs in a few days

iruzevic avatar Jun 05 '24 08:06 iruzevic