MultisiteDirectoryResolver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 14
Bugs 8 Features 5
Metric Value
wmc 12
c 14
b 8
f 5
lcom 1
cbo 1
dl 0
loc 105
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A fixSiteUrlFilter() 0 11 2
B fixStyleScriptPathFilter() 0 19 5
A fixWpIncludeFolder() 0 11 2
A init() 0 7 1
1
<?php
2
namespace Gwa\Wordpress;
3
4
use Gwa\Wordpress\Contracts\MultisiteDirectoryResolver as ResolverContract;
5
6
class MultisiteDirectoryResolver extends AbstractResolver implements ResolverContract
7
{
8
    /**
9
     * Folder path to wordpress, with trailing slash.
10
     *
11
     * @var string
12
     */
13
    protected $wpDirectoryPath = '';
14
15
    /**
16
     * Wordpress folder name.
17
     *
18
     * @var string
19
     */
20
    protected $wpFolderName = '';
21
22
    /**
23
     * MultisiteDirectoryResolver.
24
     *
25
     * @param string $wpdir
26
     */
27
    public function __construct($wpdir)
28
    {
29
        $this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';
30
31
        $this->setWpFolderName();
32
    }
33
34
    /**
35
     * Set the right path for wp-login and wp-admin.
36
     *
37
     * @param string      $url
38
     * @param string      $path
39
     * @param string|null $scheme
40
     *
41
     * @return string
42
     */
43
    public function fixSiteUrlFilter($url, $path, $scheme)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $scheme is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        if (strpos($url, $this->wpDirectoryPath)) {
46
            return $url;
47
        }
48
49
        $wordpressUrl = ['/(wp-login\.php)/', '/(wp-admin)/'];
50
        $multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-login.php', trim($this->wpDirectoryPath, '/').'/wp-admin'];
51
52
        return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1);
53
    }
54
55
    /**
56
     * Set the right path for script and style loader.
57
     *
58
     * @param string $src
59
     * @param string $handle
60
     *
61
     * @return string
62
     */
63
    public function fixStyleScriptPathFilter($src, $handle)
64
    {
65
        $dir = rtrim($this->wpDirectoryPath, '/');
66
67
        if (
68
            strpos($src, $this->getWpBridge()->siteUrl()) !== false &&
69
            strpos($src, 'plugins') === false &&
70
            strpos($src, $dir) === false
71
        ) {
72
            $styleUrl = explode($this->getWpBridge()->siteUrl(), $src);
73
            $src = $this->getWpBridge()->siteUrl().'/'.$dir.$styleUrl[1];
74
        }
75
76
        if (strpos($src, '/app')) {
77
            $src = str_replace('//app', '/app', $src);
78
        }
79
80
        return $this->getWpBridge()->escUrl($src);
81
    }
82
83
    /**
84
     * Add subfolder path to wp-includes path.
85
     *
86
     * @param string $url
87
     * @param string $path
88
     *
89
     * @return string
90
     */
91
    public function fixWpIncludeFolder($url, $path)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        if (strpos($url, $this->wpDirectoryPath)) {
94
            return $url;
95
        }
96
97
        $wordpressUrl = ['/(wp-includes)/'];
98
        $multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-includes'];
99
100
        return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1);
101
    }
102
103
    public function init()
104
    {
105
        parent::init();
106
107
        $this->getWpBridge()->addFilter('site_url', [$this, 'fixSiteUrlFilter'], 10, 3);
108
        $this->getWpBridge()->addFilter('includes_url', [$this, 'fixWpIncludeFolder'], 10, 2);
109
    }
110
}
111