Completed
Branch FET/11183/improvements-to-pue-... (dfe6b8)
by
unknown
75:16 queued 60:57
created

LicenseService::isUpdateAvailable()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 12
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\licensing;
3
4
use EventEspresso\core\domain\services\pue\Stats;
5
use EventEspresso\core\domain\services\pue\Config;
6
use PluginUpdateEngineChecker;
7
8
/**
9
 * LicenseService
10
 *
11
 * @package EventEspresso\core\services\licensing
12
 * @author  Darren Ethier
13
 * @since   $VID:$
14
 */
15
class LicenseService
16
{
17
    /**
18
     * @var Config
19
     */
20
    private $config;
21
22
23
    /**
24
     * @var Stats
25
     */
26
    private $stats_collection;
27
28
    public function __construct(Stats $stats_collection, Config $config)
29
    {
30
        $this->config = $config;
31
        $this->stats_collection = $stats_collection;
32
        $this->loadPueClient();
33
    }
34
35
    private function loadPueClient()
36
    {
37
        // PUE Auto Upgrades stuff
38
        if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { //include the file
39
            require_once(EE_THIRD_PARTY . 'pue/pue-client.php');
40
41
            //$options needs to be an array with the included keys as listed.
42
            $options = array(
43
                //	'optionName' => '', //(optional) - used as the reference for saving update information in the
44
                // clients options table.  Will be automatically set if left blank.
45
                'apikey'                => $this->config->siteLicenseKey(),
46
                //(required), you will need to obtain the apikey that the client gets from your site and
47
                // then saves in their sites options table (see 'getting an api-key' below)
48
                'lang_domain'           => $this->config->i18nDomain(),
49
                //(optional) - put here whatever reference you are using for the localization of your plugin (if it's
50
                // localized).  That way strings in this file will be included in the translation for your plugin.
51
                'checkPeriod'           => $this->config->checkPeriod(),
52
                //(optional) - use this parameter to indicate how often you want the client's install to ping your
53
                // server for update checks.  The integer indicates hours.  If you don't include this parameter it will
54
                // default to 12 hours.
55
                'option_key'            => $this->config->optionKey(),
56
                //this is what is used to reference the api_key in your plugin options.  PUE uses this to trigger
57
                // updating your information message whenever this option_key is modified.
58
                'options_page_slug'     => $this->config->optionsPageSlug(),
59
                'plugin_basename'       => EE_PLUGIN_BASENAME,
60
                'use_wp_update'         => true,
61
                //if TRUE then you want FREE versions of the plugin to be updated from WP
62
                'extra_stats'           => $this->stats_collection->statsCallback(),
63
                'turn_on_notices_saved' => true,
64
            );
65
            //initiate the class and start the plugin update engine!
66
            new PluginUpdateEngineChecker(
67
                $this->config->hostServerUrl(),
68
                $this->config->pluginSlug(),
69
                $options
70
            );
71
        }
72
    }
73
74
75
    /**
76
     * This is a handy helper method for retrieving whether there is an update available for the given plugin.
77
     *
78
     * @param  string $basename Use the equivalent result from plugin_basename() for this param as WP uses that to
79
     *                          identify plugins. Defaults to core update
80
     * @return boolean           True if update available, false if not.
81
     */
82
    public static function isUpdateAvailable($basename = '')
83
    {
84
        $basename = ! empty($basename) ? $basename : EE_PLUGIN_BASENAME;
85
86
        $update = false;
87
88
        // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core"
89
        $folder = DS . dirname($basename);
90
91
        $plugins = get_plugins($folder);
92
        $current = get_site_transient('update_plugins');
93
94
        foreach ((array)$plugins as $plugin_file => $plugin_data) {
95
            if (isset($current->response['plugin_file'])) {
96
                $update = true;
97
            }
98
        }
99
100
        //it's possible that there is an update but an invalid site-license-key is in use
101
        if (get_site_option('pue_json_error_' . $basename)) {
102
            $update = true;
103
        }
104
105
        return $update;
106
    }
107
}