Skip to content

Commit

Permalink
Add get_plugin_type() method and implement it via the register()
Browse files Browse the repository at this point in the history
…method.
  • Loading branch information
jrfnl committed May 3, 2015
1 parent 074a6e2 commit 8d06f76
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,18 @@ public function register( $plugin ) {
);

// Prepare the received data
$plugin = wp_parse_args( $plugin, $defaults );
$plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
$plugin = wp_parse_args( $plugin, $defaults );

// Forgive users for using string versions of booleans or floats for version nr
$plugin['version'] = (string) $plugin['version'];
$plugin['required'] = TGM_Utils::validate_bool( $plugin['required'] );
$plugin['force_activation'] = TGM_Utils::validate_bool( $plugin['force_activation'] );
$plugin['force_deactivation'] = TGM_Utils::validate_bool( $plugin['force_deactivation'] );
$plugin['version'] = (string) $plugin['version'];
$plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
$plugin['required'] = TGM_Utils::validate_bool( $plugin['required'] );
$plugin['force_activation'] = TGM_Utils::validate_bool( $plugin['force_activation'] );
$plugin['force_deactivation'] = TGM_Utils::validate_bool( $plugin['force_deactivation'] );

// Enrich the received data
$plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
$plugin['type'] = $this->get_plugin_type( $plugin['source'] );

// Set the class properties
$this->plugins[ $plugin['slug'] ] = $plugin;
Expand All @@ -1032,6 +1036,29 @@ public function register( $plugin ) {
}
}

/**
* Determine what type of source the plugin comes from.
*
* @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
* (= bundled) or an external url.
* @return string 'repo', 'external', or 'bundled'
*/

This comment has been minimized.

Copy link
@GaryJones

GaryJones May 3, 2015

Member

"type" is a very generic word in coding - ambiguous, especially when associated with a whole plugin. Could we be a bit more specific, at the expense of a few more bytes and call this get_plugin_source_type() or get_source_type(). Maybe even remove the word "type" altogether, i.e. get_source_category() and then have all of the array key and relevant variables updated to be ['source_category'] / $source_category? That's a more descriptive property than just "type" IMO.

This comment has been minimized.

Copy link
@jrfnl

jrfnl May 3, 2015

Author Contributor

Good point - it was already creating some confusion. Changed it to source_type now

protected function get_plugin_type( $source ) {

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

This comment has been minimized.

Copy link
@GaryJones

GaryJones May 3, 2015

Member

To avoid breaking code standards checks, the description could all go above the if:

// Is this a WPORG repo plugin, external package URL or bundled plugin?
if ( ... ) {
    return ...;
} elseif ( ... ) {
    return ...;
} else {
    return ...;
}

Note the slight change in language - keeping to the one technical term of "bundled" instead of "pre-packaged" (since all plugins tend to be "pre-packaged" into a zip), capitals for `"URL" and clarifying this is the WPORG repo.

This comment has been minimized.

Copy link
@jrfnl

jrfnl May 3, 2015

Author Contributor

Those comments came from the place where the code was originally placed (including the layout). I've now actually removed them all together as it's a small function and the function description contains that info already.

// Is this an external package url ?
elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
return 'external';
}
// This must be a bundled/pre-packaged plugin.
else {
return 'bundled';
}
}

/**
* Amend default configuration settings.
*
Expand Down

0 comments on commit 8d06f76

Please sign in to comment.