Skip to content

Commit

Permalink
Autoloader: refactor Plugins_Handler and add unit tests (#15516)
Browse files Browse the repository at this point in the history
* Autoloader: refactor a few methods in Plugins_Handler

Refactor a few methods in Plugins_Handler to make the class easier
to test.

* Autoloader: add unit tests for Plugins_Handler
  • Loading branch information
kbrown9 authored and kraftbj committed Jun 22, 2020
1 parent 80a8710 commit e7983fe
Show file tree
Hide file tree
Showing 3 changed files with 405 additions and 9 deletions.
27 changes: 21 additions & 6 deletions packages/autoloader/src/class-plugins-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ class Plugins_Handler {
* @return Array An array of plugin names as strings.
*/
public function get_all_active_plugins( $skip_single_file_plugins = true ) {
$active_plugins = array_merge(
is_multisite()
? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
: array(),
(array) get_option( 'active_plugins', array() )
);
$active_plugins = array_merge( $this->get_multisite_plugins(), $this->get_active_plugins() );

$plugins = array_unique( array_merge( $active_plugins, $this->get_all_activating_plugins() ) );

Expand All @@ -31,6 +26,26 @@ public function get_all_active_plugins( $skip_single_file_plugins = true ) {
return $plugins;
}

/**
* Get the active sitewide plugins in a multisite environment.
*
* @return Array The active sitewide plugins.
*/
protected function get_multisite_plugins() {
return is_multisite()
? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
: array();
}

/**
* Get the currently active plugins.
*
* @return Array The active plugins.
*/
protected function get_active_plugins() {
return (array) get_option( 'active_plugins', array() );
}

/**
* Ensure the plugin has its own directory and not a single-file plugin.
*
Expand Down
59 changes: 56 additions & 3 deletions packages/autoloader/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,62 @@ function trailingslashit( $string ) {
}
}

/*
* Load the composer autoloaders.
*/
if ( ! function_exists( 'wp_unslash' ) ) {
/**
* A drop-in for a WordPress core function.
*
* @param string|string[] $value String or array of strings to unslash.
* @return string|string[] Unslashed $value
*/
function wp_unslash( $value ) {
return stripslashes_deep( $value );
}

/**
* A drop-in for a WordPress core function.
*
* @param mixed $value The value to be stripped.
* @return mixed Stripped value.
*/
function stripslashes_deep( $value ) {
return map_deep( $value, 'stripslashes_from_strings_only' );
}

/**
* A drop-in for a WordPress core function.
*
* @param mixed $value The array, object, or scalar.
* @param callable $callback The function to map onto $value.
* @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
*/
function map_deep( $value, $callback ) {
if ( is_array( $value ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = map_deep( $item, $callback );
}
} elseif ( is_object( $value ) ) {
$object_vars = get_object_vars( $value );
foreach ( $object_vars as $property_name => $property_value ) {
$value->$property_name = map_deep( $property_value, $callback );
}
} else {
$value = call_user_func( $callback, $value );
}

return $value;
}

/**
* A drop-in for a WordPress core function.
*
* @param mixed $value The array or string to be stripped.
* @return mixed $value The stripped value.
*/
function stripslashes_from_strings_only( $value ) {
return is_string( $value ) ? stripslashes( $value ) : $value;
}
}

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../src/functions.php';
require_once __DIR__ . '/../../src/class-plugins-handler.php';
Expand Down
Loading

0 comments on commit e7983fe

Please sign in to comment.