public function process_request()
{
try {
$show_updates = false;
$json_encode_flags = 128;
// 128 == JSON_PRETTY_PRINT
if (defined('JSON_PRETTY_PRINT')) {
$json_encode_flags = JSON_PRETTY_PRINT;
}
if (!isset($_REQUEST['key']) || $_REQUEST['key'] != get_site_option('github_updater_api_key')) {
throw new \Exception('Bad api key.');
}
$tag = 'master';
if (isset($_REQUEST['tag'])) {
$tag = $_REQUEST['tag'];
} elseif (isset($_REQUEST['committish'])) {
$tag = $_REQUEST['committish'];
}
/**
* Parse webhook response and convert 'tag' to 'committish'.
* This will avoid potential race conditions.
*
* Throw Exception if `$_REQUEST['tag'] !== webhook branch` this avoids
* unnecessary updates for PUSH to different branch.
*/
$webhook_response = $this->get_webhook_data();
if ($webhook_response) {
if ($tag === $webhook_response['branch']) {
$tag = $webhook_response['hash'];
} else {
throw new \Exception('Request tag and webhook are not matching.');
}
}
if (isset($_REQUEST['plugin'])) {
$this->update_plugin($_REQUEST['plugin'], $tag);
} elseif (isset($_REQUEST['theme'])) {
$this->update_theme($_REQUEST['theme'], $tag);
} elseif (isset($_REQUEST['updates'])) {
$show_updates = true;
} else {
throw new \Exception('No plugin or theme specified for update.');
}
} catch (\Exception $e) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(array('message' => $e->getMessage(), 'error' => true), $json_encode_flags);
exit;
}
header('Content-Type: application/json');
$response = array('messages' => $this->get_messages());
if ($show_updates) {
$response = $this->show_updates($response);
}
if ($this->is_error()) {
$response['error'] = true;
http_response_code(500);
} else {
$response['success'] = true;
}
echo json_encode($response, $json_encode_flags) . "\n";
exit;
}