1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Updater
  7: {
  8:       9:  10:  11: 
 12:     const CACHE_TIME = 43200; 
 13: 
 14:      15:  16: 
 17:     protected $api;
 18: 
 19:      20:  21: 
 22:     protected $license;
 23: 
 24:     public function __construct(Quform_Api $api, Quform_License $license)
 25:     {
 26:         $this->api = $api;
 27:         $this->license = $license;
 28:     }
 29: 
 30:      31:  32:  33:  34:  35: 
 36:     public function setUpdateTransient($transient)
 37:     {
 38:         if (empty($transient) || !is_object($transient)) {
 39:             return $transient;
 40:         }
 41: 
 42:         $latestVersionInfo = $this->getLatestVersionInfo();
 43: 
 44:         if ($latestVersionInfo && version_compare(QUFORM_VERSION, $latestVersionInfo->new_version, '<')) {
 45:             $transient->response[QUFORM_BASENAME] = $latestVersionInfo;
 46:         } else {
 47:             unset($transient->response[QUFORM_BASENAME]);
 48: 
 49:             $transient->no_update[QUFORM_BASENAME] = (object) array(
 50:                 'id' => QUFORM_NAME,
 51:                 'slug' => QUFORM_NAME,
 52:                 'plugin' => QUFORM_BASENAME,
 53:                 'new_version' => QUFORM_VERSION,
 54:                 'url' => 'https://www.quform.com/',
 55:                 'package' => ''
 56:             );
 57:         }
 58: 
 59:         return $transient;
 60:     }
 61: 
 62:      63:  64: 
 65:     protected function validateCheckForUpdateRequest()
 66:     {
 67:         if ( ! current_user_can('quform_settings')) {
 68:             wp_send_json(array(
 69:                 'type'    => 'error',
 70:                 'message' => __('Insufficient permissions', 'quform')
 71:             ));
 72:         }
 73: 
 74:         if ( ! check_ajax_referer('quform_manual_update_check', false, false)) {
 75:             wp_send_json(array(
 76:                 'type'    => 'error',
 77:                 'message' => __('Nonce check failed', 'quform')
 78:             ));
 79:         }
 80:     }
 81: 
 82:      83:  84: 
 85:     public function checkForUpdate()
 86:     {
 87:         $this->validateCheckForUpdateRequest();
 88: 
 89:         delete_site_transient('quform_latest_version_info');
 90:         delete_site_transient('update_plugins');
 91: 
 92:         $latestVersionInfo = $this->getLatestVersionInfo(false);
 93: 
 94:         if ( ! $latestVersionInfo || ! isset($latestVersionInfo->new_version)) {
 95:             wp_send_json(array(
 96:                 'type' => 'error',
 97:                 'message' => __('Could not find an updated version', 'quform')
 98:             ));
 99:         }
100: 
101:         if (version_compare(QUFORM_VERSION, $latestVersionInfo->new_version, '<')) {
102:             wp_send_json(array(
103:                 'type' => 'success',
104:                 'message' => wp_kses(sprintf(
105:                     
106:                     __('An update to version %1$s is available, %2$svisit the Plugins page%3$s to update.', 'quform'),
107:                     $latestVersionInfo->new_version,
108:                     sprintf('<a href="%s">', esc_url(admin_url('plugins.php'))),
109:                     '</a>'
110:                 ), array('a' => array('href' => array())))
111:             ));
112:         } else {
113:             wp_send_json(array(
114:                 'type' => 'success',
115:                 'message' => __('You are using the latest version.', 'quform')
116:             ));
117:         }
118:     }
119: 
120:     121: 122: 123: 124: 125: 
126:     protected function getLatestVersionInfo($cache = true)
127:     {
128:         $latestVersionInfo = $cache ? get_site_transient('quform_latest_version_info') : false;
129: 
130:         if ( ! $latestVersionInfo) {
131:             
132:             $response = $this->api->get('/update-check', array(
133:                 'license_key' => $this->license->getKey(),
134:                 'site_url' => Quform::base64UrlEncode(site_url())
135:             ));
136: 
137:             if (is_array($response)) {
138:                 if (isset($response['revoke'])) {
139:                     $this->license->revoke();
140:                     unset($response['revoke']);
141:                 }
142: 
143:                 $latestVersionInfo = (object) $response;
144:                 $latestVersionInfo->slug = QUFORM_NAME;
145:                 $latestVersionInfo->plugin = QUFORM_BASENAME;
146: 
147:                 set_site_transient('quform_latest_version_info', $latestVersionInfo, self::CACHE_TIME);
148:             }
149:         }
150: 
151:         return $latestVersionInfo;
152:     }
153: 
154:     155: 156: 157: 158: 159: 160: 161: 
162:     public function pluginInformation($false, $action, $args)
163:     {
164:         
165:         if ($action != 'plugin_information' || ! isset($args->slug) || $args->slug != QUFORM_NAME) {
166:             return $false;
167:         }
168: 
169:         $response = $this->api->get('/plugin-information', array(
170:             'license_key' => $this->license->getKey(),
171:             'site_url' => Quform::base64UrlEncode(site_url())
172:         ));
173: 
174:         if ( ! is_array($response)) {
175:             return $false;
176:         }
177: 
178:         $response['slug'] = QUFORM_NAME;
179: 
180:         return (object) $response;
181:     }
182: }
183: