Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract duplicate code to retrieve a download url #373

Merged
merged 2 commits into from
Apr 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 76 additions & 48 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,32 +537,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 @@ -571,11 +554,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 @@ -1127,6 +1107,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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been bugging me.

Cleaner here would be:

$dump = ( true === WP_DEBUG ) ? var_dump( $api ) : ''; //wpcs: xss ok
wp_die( esc_html( $this->strings['oops'] ) . $dump );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will cause havoc for anyone who's got the Xdebug extension running as xdebug will prettify the output using html.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My code should be no different - just refactoring out multiple calls to wp_die(), esc_html() and array value access.

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 @@ -1678,7 +1728,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 @@ -1688,28 +1737,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