Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVEMENT] - Getting project version and name won't work in the case of a plugin #386

Open
dingo-d opened this issue Nov 20, 2023 · 2 comments
Assignees
Labels
feature request New feature request improvement Small improvement fixes, either readability or performance improvements

Comments

@dingo-d
Copy link
Collaborator

dingo-d commented Nov 20, 2023

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.

return \wp_get_theme('', \dirname(__DIR__, 3))->get('TextDomain');

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.

@iruzevic
Copy link
Member

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

@dingo-d
Copy link
Collaborator Author

dingo-d commented Nov 20, 2023

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 🤷🏼‍♂️

@iruzevic iruzevic linked a pull request Apr 27, 2024 that will close this issue
@iruzevic iruzevic changed the title Getting project version and name won't work in the case of a plugin [IMPROVEMENT] - Getting project version and name won't work in the case of a plugin Apr 28, 2024
@iruzevic iruzevic self-assigned this Apr 28, 2024
@iruzevic iruzevic added improvement Small improvement fixes, either readability or performance improvements feature request New feature request labels Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature request improvement Small improvement fixes, either readability or performance improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants