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.
MIT License
2.22k stars 403 forks source link

How to send additional information from server to plugin checker #327

Open ZdenekWPlama opened 4 years ago

ZdenekWPlama commented 4 years ago

Hi,

I'm writing about plugin update checker (https://github.com/YahnisElsts/plugin-update-checker)

Its perfect solution for my purpose but I stuck on this problem:

I need to get variable (loaded on WP Update Server) from server to wordpress plugins page. With this variable I need to check licence (if exist/ if valid / if expired). On check I use script from this article https://w-shadow.com/blog/2013/03/19/plugin-updates-securing-download-links/

Can you please advice me how to create such variable and how to send it to plugins page?

Thank you very much, Zdenek

YahnisElsts commented 4 years ago

On the plugin side, you could use the puc_request_info_result-$slug filter to process the data received from the server and store your custom variable somewhere. Here's a simplified version of the code I use in one of my paid plugins:

class LicenseManager {
    function __construct() {
        //Let's assume that $updateChecker is created or passed in somehow.
        //PUC has a utility function addFilter() that will automatically add the "puc_" prefix
        //and the necessary "-$slug" suffix to the filter name.
        $updateChecker->addFilter(
            'request_info_result', 
            array($this, 'refreshLicenseFromPluginInfo'), 
            10, 
            2
        );
    }

    //...

    /**
     * @param Puc_v4p8_Plugin_Info|null $pluginInfo
     * @param array $result The response returned by wp_remote_get()
     * @return Puc_v4p8_Plugin_Info|null
     */    
    public function refreshLicenseFromPluginInfo($pluginInfo, $result) {
        //Verify that this is an OK response.
        if ( !is_wp_error($result) 
            && isset($result['response']['code']) 
            && ($result['response']['code'] == 200) 
            && !empty($result['body']) 
        ) {
            $apiResponse = json_decode($result['body']);
            if ( is_object($apiResponse) && isset($apiResponse->customVariable) ) {
                //Save the custom data.
                $this->licenseDetails = $this->createLicenseObject($apiResponse->customVariable);
                $this->saveLicense();
            }
        }
        //Return the plugin metadata unmodified.
        return $pluginInfo;
    }
}
ZdenekWPlama commented 4 years ago

Thank you for your advice, it was very helpful.