-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-mu-plugins-url-everywhere.php
83 lines (73 loc) · 2.1 KB
/
wp-mu-plugins-url-everywhere.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
Plugin Name: Plugins URL Everywhere
Description: Allow Plugins URL function to create urls towards any directory
Version: 1.0
Author: Sebastian Thulin
*/
namespace WPMUPluginsURLEverywhere;
/**
* Class WPMUPluginsURLEverywhere
*
* Main class for the Plugins URL Everywhere plugin.
*/
class WPMUPluginsURLEverywhere
{
/**
* Constructor for the WPMUPluginsURLEverywhere class.
* Registers the 'init' action hook to initialize the filter.
*/
public function __construct()
{
add_action('init', [$this, 'initFilter'], 2);
}
/**
* Initialize the filter by registering the 'plugins_url' filter hook.
*/
public function initFilter() {
add_filter('plugins_url', [$this, 'filterPluginsUrl'], 10, 3);
}
/**
* Filter the plugins URL to remove the document root and specified prefixes.
*
* @param string $url The URL to be filtered.
* @param string $path The path to the requested resource.
* @param string $plugin The plugin file path.
*
* @return string The filtered URL.
*/
public function filterPluginsUrl($url, $path, $plugin) {
$documentRoot = $this->getDocumentRoot();
if($documentRoot) {
$url = $this->removePath($url, $documentRoot);
}
return $url;
}
/**
* Remove specified prefixes from the URL.
*
* @param string $url The URL to be modified.
* @param string $documentRoot The document root path.
* @param array $prefixes An array of prefixes to remove.
*
* @return string The modified URL.
*/
private function removePath($url, $documentRoot, $prefixes = ['/wp-content/plugins']) {
foreach($prefixes as $prefix) {
$url = str_replace($prefix . $documentRoot, "", $url);
}
return $url;
}
/**
* Get the document root path from the server environment.
*
* @return string|false The document root path or false if not available.
*/
private function getDocumentRoot () {
if(isset($_SERVER['DOCUMENT_ROOT'])) {
return $_SERVER['DOCUMENT_ROOT'];
}
return false;
}
}
new \WPMUPluginsURLEverywhere\WPMUPluginsURLEverywhere();