Use new code style
This commit is contained in:
@ -22,24 +22,23 @@ class WP_Config {
|
||||
use \WP_Ultimo\Traits\Singleton;
|
||||
|
||||
/**
|
||||
* Inject the constant into the wp-config.php file.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param string $constant The name of the constant. e.g. WP_ULTIMO_CONSTANT.
|
||||
* @param string|int $value The value of that constant.
|
||||
* @return bool|\WP_Error
|
||||
*/
|
||||
public function inject_wp_config_constant($constant, $value) {
|
||||
* Inject the constant into the wp-config.php file.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param string $constant The name of the constant. e.g. WP_ULTIMO_CONSTANT.
|
||||
* @param string|int $value The value of that constant.
|
||||
* @return bool|\WP_Error
|
||||
*/
|
||||
public function inject_wp_config_constant($constant, $value) {
|
||||
|
||||
$config_path = $this->get_wp_config_path();
|
||||
|
||||
if (!is_writeable($config_path)) {
|
||||
if ( ! is_writeable($config_path)) {
|
||||
|
||||
// translators: %s is the file name.
|
||||
return new \WP_Error('not-writeable', sprintf(__('The file %s is not writable', 'wp-ultimo'), $config_path));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$config = file($config_path);
|
||||
|
||||
@ -53,32 +52,24 @@ class WP_Config {
|
||||
$hook_line = $this->find_reference_hook_line($config);
|
||||
|
||||
if ($hook_line === false) {
|
||||
|
||||
return new \WP_Error('unknown-wpconfig', __("WP Multisite WaaS can't recognize your wp-config.php, please revert it to original state for further process.", 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$config = $this->inject_contents($config, $hook_line + 1, PHP_EOL . $content . PHP_EOL);
|
||||
|
||||
return file_put_contents($config_path, implode('', $config), LOCK_EX);
|
||||
|
||||
} else {
|
||||
|
||||
list($value, $line) = $line;
|
||||
|
||||
if ($value !== true) {
|
||||
|
||||
$config[$line] = $content . PHP_EOL;
|
||||
$config[ $line ] = $content . PHP_EOL;
|
||||
|
||||
return file_put_contents($config_path, implode('', $config), LOCK_EX);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
} // end inject_wp_config_constant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually inserts the new lines into the array of wp-config.php lines.
|
||||
@ -92,17 +83,14 @@ class WP_Config {
|
||||
*/
|
||||
public function inject_contents($content_array, $line, $value) {
|
||||
|
||||
if (!is_array($value)) {
|
||||
|
||||
if ( ! is_array($value)) {
|
||||
$value = array($value);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
array_splice($content_array, $line, 0, $value);
|
||||
|
||||
return $content_array;
|
||||
|
||||
} // end inject_contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the correct path to the wp-config.php file.
|
||||
@ -113,20 +101,13 @@ class WP_Config {
|
||||
public function get_wp_config_path() {
|
||||
|
||||
if (file_exists(ABSPATH . 'wp-config.php')) {
|
||||
|
||||
return (ABSPATH . 'wp-config.php');
|
||||
|
||||
} elseif (@file_exists(dirname(ABSPATH) . '/wp-config.php') && !@file_exists(dirname(ABSPATH) . '/wp-settings.php')) {
|
||||
|
||||
} elseif (@file_exists(dirname(ABSPATH) . '/wp-config.php') && ! @file_exists(dirname(ABSPATH) . '/wp-settings.php')) {
|
||||
return (dirname(ABSPATH) . '/wp-config.php');
|
||||
|
||||
} elseif (defined('WP_TESTS_MULTISITE') && constant('WP_TESTS_MULTISITE') === true) {
|
||||
|
||||
return '/tmp/wordpress-tests-lib/wp-tests-config.php';
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end get_wp_config_path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find reference line for injection.
|
||||
@ -159,34 +140,30 @@ class WP_Config {
|
||||
* A negative number of lines can be passed to write before the found line,
|
||||
* instead of writing after it.
|
||||
*/
|
||||
$patterns = apply_filters('wu_wp_config_reference_hook_line_patterns', array(
|
||||
'/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
|
||||
'/^( ){0,}\$table_prefix\s*=.*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
|
||||
'/(\/\* That\'s all, stop editing! Happy publishing\. \*\/)/' => -2,
|
||||
'/<\?php/' => 0,
|
||||
));
|
||||
$patterns = apply_filters(
|
||||
'wu_wp_config_reference_hook_line_patterns',
|
||||
array(
|
||||
'/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
|
||||
'/^( ){0,}\$table_prefix\s*=.*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
|
||||
'/(\/\* That\'s all, stop editing! Happy publishing\. \*\/)/' => -2,
|
||||
'/<\?php/' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
$line = 1;
|
||||
|
||||
foreach ($patterns as $pattern => $lines_to_add) {
|
||||
|
||||
foreach ($config as $k => $line) {
|
||||
|
||||
if (preg_match($pattern, (string) $line)) {
|
||||
|
||||
$line = $k + $lines_to_add;
|
||||
|
||||
break 2;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $line;
|
||||
|
||||
} // end find_reference_hook_line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert the injection of a constant in wp-config.php
|
||||
@ -200,23 +177,19 @@ class WP_Config {
|
||||
|
||||
$config_path = $this->get_wp_config_path();
|
||||
|
||||
if (!is_writeable($config_path)) {
|
||||
if ( ! is_writeable($config_path)) {
|
||||
|
||||
// translators: %s is the file name.
|
||||
return new \WP_Error('not-writeable', sprintf(__('The file %s is not writable', 'wp-ultimo'), $config_path));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$config = file($config_path);
|
||||
|
||||
$line = $this->find_injected_line($config, $constant);
|
||||
|
||||
if ($line === false) {
|
||||
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
$value = $line[0];
|
||||
|
||||
$line = $line[1];
|
||||
@ -224,41 +197,32 @@ class WP_Config {
|
||||
if ($value === 'true' || $value === '1') {
|
||||
|
||||
// value is true, we will remove this
|
||||
unset($config[$line]);
|
||||
unset($config[ $line ]);
|
||||
|
||||
// save it
|
||||
return file_put_contents($config_path, implode('', $config), LOCK_EX);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end revert;
|
||||
/**
|
||||
* Checks for the injected line inside of the wp-config.php file.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $config Array containing the lines of the config file, for searching.
|
||||
* @param string $constant The constant name.
|
||||
* @return mixed[]|bool
|
||||
*/
|
||||
public function find_injected_line($config, $constant) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Checks for the injected line inside of the wp-config.php file.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $config Array containing the lines of the config file, for searching.
|
||||
* @param string $constant The constant name.
|
||||
* @return mixed[]|bool
|
||||
*/
|
||||
public function find_injected_line($config, $constant) {
|
||||
|
||||
$pattern = "/^define\(\s*['|\"]" . $constant . "['|\"],(.*)\)/";
|
||||
|
||||
foreach ($config as $k => $line) {
|
||||
|
||||
if (preg_match($pattern, (string) $line, $matches)) {
|
||||
|
||||
return array(trim($matches[1]), $k);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
} // end find_injected_line;
|
||||
|
||||
} // end class WP_Config;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user