Skip to content

Commit cc5d0f4

Browse files
committed
Little optimizations.
1 parent bd2ffd7 commit cc5d0f4

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/WP_Super_Cache_CLI_Loader.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function init_cache_base() {
8686

8787
// If the parameter --url doesn't exist then gets HTTP_HOST from WordPress Address.
8888
if ( empty( $_SERVER['HTTP_HOST'] ) ) {
89-
$_SERVER['HTTP_HOST'] = (string) parse_url( get_option( 'home' ), PHP_URL_HOST );
89+
$_SERVER['HTTP_HOST'] = $this->parse_home_url( PHP_URL_HOST );
9090
}
9191

9292
if ( empty( $WPSC_HTTP_HOST ) ) {
@@ -114,15 +114,13 @@ private function maybe_load_config() {
114114
|| empty( $wp_cache_config_file_sample )
115115
|| ! $this->maybe_include_file( 'include', $wp_cache_config_file_sample )
116116
) {
117-
WP_CLI::error( 'Can not load cache config file.' );
117+
WP_CLI::error( 'Cannot load cache config file.' );
118118
}
119119

120120
WP_CLI::warning( 'Default cache config file loaded - ' . str_replace( ABSPATH, '', $wp_cache_config_file_sample ) );
121121
}
122122

123-
$wp_cache_home_path = function_exists( 'wp_parse_url' ) ?
124-
trailingslashit( (string) wp_parse_url( get_option( 'home' ), PHP_URL_PATH ) ) :
125-
trailingslashit( (string) parse_url( get_option( 'home' ), PHP_URL_PATH ) );
123+
$wp_cache_home_path = trailingslashit( $this->parse_home_url( PHP_URL_PATH ) );
126124
}
127125

128126
/**
@@ -272,7 +270,7 @@ private function maybe_include_file( $func, $filename, $run = '' ) {
272270
}
273271

274272
if ( $loaded && ! empty( $run ) && function_exists( $run ) ) {
275-
call_user_func_array( $run, array() );
273+
call_user_func( $run );
276274
}
277275

278276
return $loaded;
@@ -281,6 +279,8 @@ private function maybe_include_file( $func, $filename, $run = '' ) {
281279
/**
282280
* Gets version of WP Super Cache.
283281
*
282+
* @global string $wp_cache_config_file_sample Absolute path to wp-cache config sample file.
283+
*
284284
* @return string
285285
*/
286286
public function get_wpsc_version() {
@@ -298,15 +298,18 @@ public function get_wpsc_version() {
298298
require_once ABSPATH . 'wp-admin/includes/plugin.php';
299299
}
300300

301-
$this->wpsc_plugin_file = trailingslashit( WP_PLUGIN_DIR ) . 'wp-super-cache/wp-cache.php';
302-
if ( ! empty( $wp_cache_config_file_sample ) ) {
303-
$this->wpsc_plugin_file = plugin_dir_path( $wp_cache_config_file_sample ) . 'wp-cache.php';
301+
$this->wpsc_version = '';
302+
$this->wpsc_plugin_file = empty( $wp_cache_config_file_sample )
303+
? trailingslashit( WP_PLUGIN_DIR ) . 'wp-super-cache/wp-cache.php'
304+
: plugin_dir_path( $wp_cache_config_file_sample ) . 'wp-cache.php';
305+
306+
if ( ! is_file( $this->wpsc_plugin_file ) || ! is_readable( $this->wpsc_plugin_file ) ) {
307+
return $this->wpsc_version;
304308
}
305309

306-
$this->wpsc_version = '';
307-
if ( is_file( $this->wpsc_plugin_file ) && is_readable( $this->wpsc_plugin_file ) ) {
308-
$plugin_details = get_plugin_data( $this->wpsc_plugin_file );
309-
$this->wpsc_version = ! empty( $plugin_details['Version'] ) ? $plugin_details['Version'] : '';
310+
$plugin_details = get_plugin_data( $this->wpsc_plugin_file );
311+
if ( ! empty( $plugin_details['Version'] ) ) {
312+
$this->wpsc_version = $plugin_details['Version'];
310313
}
311314

312315
return $this->wpsc_version;
@@ -324,4 +327,17 @@ private function is_wpsc_plugin_active() {
324327

325328
return false;
326329
}
330+
331+
/**
332+
* Retrieves the component (PHP_URL_HOST or PHP_URL_PATH) from home URL.
333+
*
334+
* @param int $component The component to retrieve.
335+
*
336+
* @return string
337+
*/
338+
private function parse_home_url( $component ) {
339+
return function_exists( 'wp_parse_url' )
340+
? (string) wp_parse_url( get_option( 'home' ), $component )
341+
: (string) parse_url( get_option( 'home' ), $component );
342+
}
327343
}

0 commit comments

Comments
 (0)