public function install($type)
{
if (isset($_POST['option_page']) && 'github_updater_install' == $_POST['option_page']) {
if (empty($_POST['github_updater_branch'])) {
$_POST['github_updater_branch'] = 'master';
}
/*
* Exit early if no repo entered.
*/
if (empty($_POST['github_updater_repo'])) {
echo '<h3>';
esc_html_e('A repository URI is required.', 'github-updater');
echo '</h3>';
return false;
}
/*
* Transform URI to owner/repo
*/
$headers = Base::parse_header_uri($_POST['github_updater_repo']);
$_POST['github_updater_repo'] = $headers['owner_repo'];
self::$install = Settings::sanitize($_POST);
self::$install['repo'] = $headers['repo'];
/*
* Create GitHub endpoint.
* Save Access Token if present.
* Check for GitHub Self-Hosted.
*/
if ('github' === self::$install['github_updater_api']) {
if ('github.com' === $headers['host'] || empty($headers['host'])) {
$github_base = 'https://api.github.com';
$headers['host'] = 'github.com';
} else {
$github_base = $headers['base_uri'] . '/api/v3';
}
self::$install['download_link'] = $github_base . '/repos/' . self::$install['github_updater_repo'] . '/zipball/' . self::$install['github_updater_branch'];
/*
* If asset is entered install it.
*/
if (false !== stristr($headers['path'], 'releases/download')) {
self::$install['download_link'] = $headers['uri'];
}
/*
* Add access token if present.
*/
if (!empty(self::$install['github_access_token'])) {
self::$install['download_link'] = add_query_arg('access_token', self::$install['github_access_token'], self::$install['download_link']);
parent::$options[self::$install['repo']] = self::$install['github_access_token'];
} elseif (!empty(parent::$options['github_access_token']) && ('github.com' === $headers['host'] || empty($headers['host']))) {
self::$install['download_link'] = add_query_arg('access_token', parent::$options['github_access_token'], self::$install['download_link']);
} elseif (!empty(parent::$options['github_enterprise_token'])) {
self::$install['download_link'] = add_query_arg('access_token', parent::$options['github_enterprise_token'], self::$install['download_link']);
}
}
/*
* Create Bitbucket endpoint and instantiate class Bitbucket_API.
* Save private setting if present.
* Ensures `maybe_authenticate_http()` is available.
*/
if ('bitbucket' === self::$install['github_updater_api']) {
self::$install['download_link'] = 'https://bitbucket.org/' . self::$install['github_updater_repo'] . '/get/' . self::$install['github_updater_branch'] . '.zip';
if (isset(self::$install['is_private'])) {
parent::$options[self::$install['repo']] = 1;
}
if (isset(self::$install['bitbucket_username'])) {
parent::$options['bitbucket_username'] = self::$install['bitbucket_username'];
}
if (isset(self::$install['bitbucket_password'])) {
parent::$options['bitbucket_password'] = self::$install['bitbucket_password'];
}
new Bitbucket_API((object) $type);
}
/*
* Create GitLab endpoint.
* Check for GitLab Self-Hosted.
*/
if ('gitlab' === self::$install['github_updater_api']) {
if ('gitlab.com' === $headers['host'] || empty($headers['host'])) {
$gitlab_base = 'https://gitlab.com';
$headers['host'] = 'gitlab.com';
} else {
$gitlab_base = $headers['base_uri'];
}
self::$install['download_link'] = implode('/', array($gitlab_base, self::$install['github_updater_repo'], 'repository/archive.zip'));
self::$install['download_link'] = add_query_arg('ref', self::$install['github_updater_branch'], self::$install['download_link']);
/*
* Add access token.
*/
if (!empty(self::$install['gitlab_access_token'])) {
self::$install['download_link'] = add_query_arg('private_token', self::$install['gitlab_access_token'], self::$install['download_link']);
parent::$options[self::$install['repo']] = self::$install['gitlab_access_token'];
if ('gitlab.com' === $headers['host']) {
parent::$options['gitlab_access_token'] = empty(parent::$options['gitlab_access_token']) ? self::$install['gitlab_access_token'] : parent::$options['gitlab_access_token'];
} else {
parent::$options['gitlab_enterprise_token'] = empty(parent::$options['gitlab_enterprise_token']) ? self::$install['gitlab_access_token'] : parent::$options['gitlab_enterprise_token'];
}
} else {
if ('gitlab.com' === $headers['host']) {
self::$install['download_link'] = add_query_arg('private_token', parent::$options['gitlab_access_token'], self::$install['download_link']);
} else {
self::$install['download_link'] = add_query_arg('private_token', parent::$options['gitlab_enterprise_token'], self::$install['download_link']);
}
}
}
parent::$options['github_updater_install_repo'] = self::$install['repo'];
if (defined('GITHUB_UPDATER_EXTENDED_NAMING') && GITHUB_UPDATER_EXTENDED_NAMING && 'plugin' === $type) {
parent::$options['github_updater_install_repo'] = implode('-', array(self::$install['github_updater_api'], $headers['owner'], self::$install['repo']));
}
update_site_option('github_updater', Settings::sanitize(parent::$options));
$url = self::$install['download_link'];
$nonce = wp_nonce_url($url);
if ('plugin' === $type) {
$plugin = self::$install['repo'];
/*
* Create a new instance of Plugin_Upgrader.
*/
$upgrader = new \Plugin_Upgrader($skin = new \Plugin_Installer_Skin(compact('type', 'title', 'url', 'nonce', 'plugin', 'api')));
add_filter('install_plugin_complete_actions', array(&$this, 'install_plugin_complete_actions'), 10, 3);
}
if ('theme' === $type) {
$theme = self::$install['repo'];
/*
* Create a new instance of Theme_Upgrader.
*/
$upgrader = new \Theme_Upgrader($skin = new \Theme_Installer_Skin(compact('type', 'title', 'url', 'nonce', 'theme', 'api')));
add_filter('install_theme_complete_actions', array(&$this, 'install_theme_complete_actions'), 10, 3);
}
/*
* Perform the action and install the plugin from the $source urldecode().
* Flush cache so we can make sure that the installed plugins/themes list is always up to date.
*/
$upgrader->install($url);
wp_cache_flush();
}
if (!isset($_POST['option_page']) || !('github_updater_install' === $_POST['option_page'])) {
$this->create_form($type);
}
return true;
}