Skip to content

Commit

Permalink
Merge pull request #373 from thomasgriffin/feature/abstract-get-downl…
Browse files Browse the repository at this point in the history
…oad-url

Abstract duplicate code to retrieve a download url
  • Loading branch information
GaryJones committed Apr 28, 2015
2 parents 9da8fb4 + 8a56596 commit fe5fa91
Showing 1 changed file with 76 additions and 48 deletions.
124 changes: 76 additions & 48 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,32 +536,15 @@ protected function do_plugin_install() {
}

// If we arrive here, we have the filesystem
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api.
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes.

$plugin['slug'] = $slug; // needed for potentially renaming of directory name
$plugin['name'] = $this->plugins[ $slug ]['name'];
$plugin['source'] = $this->plugins[ $slug ]['source'];

// Set plugin source to WordPress API link if available.
if ( 'repo' === $plugin['source'] ) {
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
$plugin['slug'] = $slug; // needed for potentially renaming of directory name
$plugin['name'] = $this->plugins[ $slug ]['name'];

if ( is_wp_error( $api ) ) {
if ( true === WP_DEBUG ) {
wp_die( esc_html( $this->strings['oops'] ) . var_dump( $api ) ); // wpcs: xss ok
} else {
wp_die( esc_html( $this->strings['oops'] ) );
}
}

if ( isset( $api->download_link ) ) {
$plugin['source'] = $api->download_link;
}
}
$source = $this->get_download_url( $slug );

// Set type, based on whether the source starts with http:// or https://.
$type = preg_match( self::EXT_REPO_REGEX, $plugin['source'] ) ? 'web' : 'upload';
$type = preg_match( self::EXT_REPO_REGEX, $source ) ? 'web' : 'upload';

// Prep variables for Plugin_Installer_Skin class.
$title = sprintf( $this->strings['installing'], $plugin['name'] );
Expand All @@ -576,11 +559,8 @@ protected function do_plugin_install() {

$nonce = 'install-plugin_' . $slug;

// Prefix a default path to pre-packaged plugins.
$source = ( 'upload' === $type ) ? $this->default_path . $plugin['source'] : $plugin['source'];

// Create a new instance of Plugin_Upgrader.
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin' ) ) );

// Perform the action and install the plugin from the $source urldecode().
add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
Expand Down Expand Up @@ -1132,6 +1112,76 @@ public function _get_plugin_data_from_name( $name, $data = 'slug' ) {

}

/**
* Retrieve the download url for a package.
*
* @since 2.5.0
*
* @param string $slug
*
* @return string Plugin download URL or path to local file or empty string if undetermined.
*/
public function get_download_url( $slug ) {

if ( empty( $this->plugins[ $slug ]['source'] ) ) {
return '';
}

$dl_source = '';
$source = $this->plugins[ $slug ]['source'];

// Is this a WP repo plugin ?
if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {

$dl_source = $this->get_wp_repo_download_url( $slug );
}
// Is this an external package url ?
elseif ( preg_match( self::EXT_REPO_REGEX, $source ) ) {

$dl_source = $source;
}
// This must be a bundled/pre-packaged plugin. Prefix it with the default path.
else {
$dl_source = $this->default_path . $source;
}

return $dl_source;
}

/**
* Retrieve the download url for a WP repo package.
*
* @since 2.5.0
*
* @param string $slug Plugin slug
*
* @return string Plugin download URL
*/
protected function get_wp_repo_download_url( $slug ) {

if ( ! function_exists( 'plugins_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api.
}

$source = '';

// Try to grab information from WordPress API
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
if ( is_wp_error( $api ) ) {
if ( true === WP_DEBUG ) {
wp_die( esc_html( $this->strings['oops'] ) . var_dump( $api ) ); // wpcs: xss ok
} else {
wp_die( esc_html( $this->strings['oops'] ) );
}

} elseif ( isset( $api->download_link ) ) {
$source = $api->download_link;
}

return $source;

}

/**
* Determine if we're on the TGMPA Install page.
*
Expand Down Expand Up @@ -1698,7 +1748,6 @@ public function process_bulk_actions() {
}

// If we arrive here, we have the filesystem
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes

// Store all information in arrays since we are processing a bulk installation.
Expand All @@ -1708,28 +1757,7 @@ public function process_bulk_actions() {
// Prepare the data for validated plugins for the install
foreach ( $plugins_to_install as $slug ) {
$name = $this->tgmpa->plugins[ $slug ]['name'];
$source = $this->tgmpa->plugins[ $slug ]['source'];

if ( 'repo' === $source || preg_match( TGM_Plugin_Activation::WP_REPO_REGEX, $source ) ) {
// Try to grab information from WordPress API
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
if ( is_wp_error( $api ) ) {
if ( true === WP_DEBUG ) {
wp_die( esc_html( $this->strings['oops'] ) . var_dump( $api ) ); // wpcs: xss ok
} else {
wp_die( esc_html( $this->strings['oops'] ) );
}
}

if ( ! is_wp_error( $api ) && isset( $api->download_link ) ) {
$source = $api->download_link;
}
unset( $api );

} elseif ( preg_match( TGM_Plugin_Activation::EXT_REPO_REGEX, $source ) !== 1 ) {
// The plugin is pre-packaged with the theme.
$source = $this->tgmpa->default_path . $source;
}
$source = $this->tgmpa->get_download_url( $slug );

if ( ! empty( $name ) && ! empty( $source ) ) {
$names[] = $name;
Expand Down

0 comments on commit fe5fa91

Please sign in to comment.