YahnisElsts / plugin-update-checker

A custom update checker for WordPress plugins. Useful if you don't want to host your project in the official WP repository, but would still like it to support automatic updates. Despite the name, it also works with themes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add query arg to requested .php file

JoPed opened this issue · comments

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.

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.

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!

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?).

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