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

Add query arg to requested .php file #549

Open
JoPed opened this issue Dec 1, 2023 · 4 comments
Open

Add query arg to requested .php file #549

JoPed opened this issue Dec 1, 2023 · 4 comments

Comments

@JoPed
Copy link

JoPed commented Dec 1, 2023

I just started using the plugin-update-checker, the thing is that there is already licens management in my plugin and for that to work I have to add a query arg to to the file specified in the buildUpdateChecker function, I need this because the existing license management is handled in that file.

For context: I'm executing the below code from my main plugin file, without using any hooks as recommended in the getting started section.

Here is the old way of how I send the license_key with the requested file.

`if ( ! function_exists( 'remote_get_info' ) ) {
function remote_get_info( $info_url, $license_key, $args = array() ) {

	$query_args = array_merge(
		array(
			'license_key' => urlencode( $license_key ),
			'domain' => $_SERVER['SERVER_NAME'],
		),
		$args
	);

	$url = add_query_arg(
		$query_args,
		$info_url
	);

	$remote = wp_remote_get(
		$url,
		array(
			'timeout' => 10,
			'headers' => array(
				'Accept' => 'application/json'
			)
		)
	);

	return $remote;
}

}`

I have tried to add it directly to the url like this:

$myUpdateChecker = PucFactory::buildUpdateChecker( 'link-to-file.php?license_key=' . urlencode( 'the-license-key-value' ), __FILE__, 'my-plugin-slug' );

And I have tried using the addQueryArgFilter function like this:

`$myUpdateChecker->addQueryArgFilter( function ($args) {
$args['license_key'] = urlencode( 'the-license-key-value' );

return $args;

} );`

This might very well just be me not properly understanding the plugin-update-checker. But can someone point me in the correct direction of achieving something similar to what I was doing with the remote_get_info function.

@YahnisElsts
Copy link
Owner

addQueryArgFilter is the correct way to do this with PUC. The code formatting is a bit messed up in your post, but at a glance, how you're doing it seems mostly correct. The only minor issue I see is that you're urlencode-ing the value: you don't need to do that here, PUC will do it automatically.

@JoPed
Copy link
Author

JoPed commented Mar 11, 2024

I know it has been some time since my initial post - but thank you for the response!
Most likely I will end up using the GitHub implementation for updating my plugin. While using the GitHub implementation, can I then still prevent plugin updates, if the user has no valid license? I already have the functionality to create and validate licenses, but I'm unsure as to how to use it together with the GitHub implementation of the plugin-update-checker.

Hope this makes sense, and that you can point me in the right direction!

@YahnisElsts
Copy link
Owner

I don't think that's really possible with GitHub. To do it securely, you need to validate the license on the server, not just in the plugin. And you can't run your custom license validation code on GitHub's API servers, so that's not going to work.

Technically, you could try something like giving each licensed user their own API token, but I don't know how practical that would be (does GitHub even allow generating arbitrarily many tokens?).

@JoPed
Copy link
Author

JoPed commented Mar 12, 2024

I feared you would say that, but thanks for the quick response. I honestly don't know if GitHub allows that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants