Merged in feature/MAW-855-import-code-into-aws (pull request #2)
code import from pantheon * code import from pantheon
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_MeasuredString {
|
||||
|
||||
public $string;
|
||||
public $length;
|
||||
|
||||
public function __construct($string) {
|
||||
$this->string = $string;
|
||||
$this->length = strlen($string);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return $this->string;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_Multisite {
|
||||
|
||||
/**
|
||||
* Returns an array of all active multisite blogs (if `$blogIds` is `null`) or a list of active multisite blogs
|
||||
* filtered to only those in `$blogIds` if non-null.
|
||||
*
|
||||
* @param array|null $blogIds
|
||||
* @return array
|
||||
*/
|
||||
public static function retrieve_active_sites($blogIds = null) {
|
||||
$args = array(
|
||||
'number' => '', /* WordPress core passes an empty string which appears to remove the result set limit */
|
||||
'update_site_meta_cache' => false, /* Defaults to true which is not desirable for this use case */
|
||||
//Ignore archived/spam/deleted sites
|
||||
'archived' => 0,
|
||||
'spam' => 0,
|
||||
'deleted' => 0
|
||||
);
|
||||
|
||||
if ($blogIds !== null) {
|
||||
$args['site__in'] = $blogIds;
|
||||
}
|
||||
|
||||
if (function_exists('get_sites')) {
|
||||
return get_sites($args);
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
if ($blogIds !== null) {
|
||||
$blogIdsQuery = implode(',', wp_parse_id_list($args['site__in']));
|
||||
return $wpdb->get_results("SELECT * FROM {$wpdb->blogs} WHERE blog_id IN ({$blogIdsQuery}) AND archived = 0 AND spam = 0 AND deleted = 0");
|
||||
}
|
||||
|
||||
return $wpdb->get_results("SELECT * FROM {$wpdb->blogs} WHERE archived = 0 AND spam = 0 AND deleted = 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_MultisiteConfigurationExtractor {
|
||||
|
||||
private $prefix, $suffix;
|
||||
private $suffixOffset;
|
||||
|
||||
public function __construct($prefix, $suffix) {
|
||||
$this->prefix = new Utility_MeasuredString($prefix);
|
||||
$this->suffix = new Utility_MeasuredString($suffix);
|
||||
$this->suffixOffset = -$this->suffix->length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a `get_user_meta` result array into a more usable format. The input array will be something similar to
|
||||
* [
|
||||
* 'wp_capabilities' => '...',
|
||||
* 'wp_3_capabilities' => '...',
|
||||
* 'wp_4_capabilities' => '...',
|
||||
* 'wp_10_capabilities' => '...',
|
||||
* ]
|
||||
*
|
||||
* This will return
|
||||
* [
|
||||
* 1 => '...',
|
||||
* 3 => '...',
|
||||
* 4 => '...',
|
||||
* 10 => '...',
|
||||
* ]
|
||||
*
|
||||
* @param array $values
|
||||
* @return array
|
||||
*/
|
||||
private function parseBlogIds($values) {
|
||||
$parsed = array();
|
||||
foreach ($values as $key => $value) {
|
||||
if (substr($key, $this->suffixOffset) === $this->suffix->string && strpos($key, (string) $this->prefix) === 0) {
|
||||
$blogId = substr($key, $this->prefix->length, strlen($key) - $this->prefix->length + $this->suffixOffset);
|
||||
if (empty($blogId)) {
|
||||
$parsed[1] = $value;
|
||||
}
|
||||
else if (substr($blogId, -1) === '_') {
|
||||
$parsed[(int) $blogId] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters $values, which is the resulting array from `$this->parseBlogIds` so it contains only the values for the
|
||||
* sites in $sites.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $sites
|
||||
* @return array
|
||||
*/
|
||||
private function filterValues($values, $sites) {
|
||||
$filtered = array();
|
||||
foreach ($sites as $site) {
|
||||
$blogId = (int) $site->blog_id;
|
||||
$filtered[$blogId] = $values[$blogId];
|
||||
}
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a `get_user_meta` result array to re-key it so the keys are the numerical ID of all multisite blog IDs
|
||||
* in `$values` that are still in an active state.
|
||||
*
|
||||
* @param array $values
|
||||
* @return array
|
||||
*/
|
||||
public function extract($values) {
|
||||
$parsed = $this->parseBlogIds($values);
|
||||
if (empty($parsed))
|
||||
return $parsed;
|
||||
$sites = Utility_Multisite::retrieve_active_sites(array_keys($parsed));
|
||||
return $this->filterValues($parsed, $sites);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_URL {
|
||||
|
||||
/**
|
||||
* Similar to WordPress' `admin_url`, this returns a host-relative URL for the given path. It may be used to avoid
|
||||
* canonicalization issues with CORS (e.g., the site is configured for the www. variant of the URL but doesn't forward
|
||||
* the other).
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function relative_admin_url($path = '') {
|
||||
$url = admin_url($path);
|
||||
$components = parse_url($url);
|
||||
$s = $components['path'];
|
||||
if (!empty($components['query'])) {
|
||||
$s .= '?' . $components['query'];
|
||||
}
|
||||
if (!empty($components['fragment'])) {
|
||||
$s .= '#' . $components['fragment'];
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user