AbstractResolver   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 15
c 4
b 1
f 3
lcom 2
cbo 1
dl 0
loc 133
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A fixNetworkAdminUrlFilter() 0 22 2
A fixWpDoubleSlashFilter() 0 10 3
A fixWpProtocolFilter() 0 12 4
A getSiteProtocol() 0 4 2
A init() 0 10 1
A setWpFolderName() 0 6 1
1
<?php
2
namespace Gwa\Wordpress;
3
4
use Gwa\Wordpress\WpBridge\Traits\WpBridgeTrait;
5
6
abstract class AbstractResolver
7
{
8
    use WpBridgeTrait;
9
10
    /**
11
     * Folder path to wordpress, with trailing slash.
12
     *
13
     * @var string
14
     */
15
    protected $wpDirectoryPath = '';
16
17
    /**
18
     * Wordpress folder name.
19
     *
20
     * @var string
21
     */
22
    protected $wpFolderName = '';
23
24
    /**
25
     * MultisiteDirectoryResolver.
26
     *
27
     * @param string $wpdir
28
     */
29
    public function __construct($wpdir)
30
    {
31
        $this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';
32
        $this->setWpFolderName();
33
    }
34
35
    /**
36
     * Set the right links in Adminbar.
37
     *
38
     * @param string $path
39
     * @param string $scheme
40
     *
41
     * @return string
42
     */
43
    public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin')
0 ignored issues
show
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($path, $this->wpDirectoryPath)) {
46
            return $path;
47
        }
48
49
        $wordpressUrl = [
50
            '/(wp-admin)/',
51
            '/(wp-login\.php)/',
52
            '/(wp-activate\.php)/',
53
            '/(wp-signup\.php)/',
54
        ];
55
56
        $multiSiteUrl = [
57
            $this->wpFolderName.'/wp-admin',
58
            $this->wpFolderName.'/wp-login.php',
59
            $this->wpFolderName.'/wp-activate.php',
60
            $this->wpFolderName.'/wp-signup.php',
61
        ];
62
63
        return preg_replace($wordpressUrl, $multiSiteUrl, $path, 1);
64
    }
65
66
    /**
67
     * Fix double backslashes in app folder.
68
     *
69
     * @param string
70
     */
71
    public function fixWpDoubleSlashFilter($urls)
72
    {
73
        foreach ($urls as &$url) {
74
            if ($url) {
75
                $url = str_replace('//app', '/app', $url);
76
            }
77
        }
78
79
        return $urls;
80
    }
81
82
    /**
83
     * Fixes the protocol in urls. Replaces leading double slashes //
84
     * with the full protocol; https or http depending on context.
85
     *
86
     * @param string
87
     *
88
     * @return array
89
     */
90
    public function fixWpProtocolFilter($urls)
91
    {
92
        $protocol = $this->getSiteProtocol();
93
94
        foreach ($urls as $k => &$v) {
95
            if ((strpos($k, 'url') !== false) && (substr($v, 0, 2) === '//')) {
96
                $v = $protocol.ltrim($v, '//');
97
            }
98
        }
99
100
        return $urls;
101
    }
102
103
    /**
104
     * Get the correct protocol.
105
     *
106
     * @return string
107
     */
108
    protected function getSiteProtocol()
109
    {
110
        return $this->getWpBridge()->isSsl() ? 'https://' : 'http://';
111
    }
112
113
    /**
114
     * Init all filter.
115
     */
116
    public function init()
117
    {
118
        $this->getWpBridge()->addFilter('network_admin_url', [$this, 'fixNetworkAdminUrlFilter'], 10, 2);
119
120
        $this->getWpBridge()->addFilter('script_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
121
        $this->getWpBridge()->addFilter('style_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
122
123
        $this->getWpBridge()->addFilter('upload_dir', [$this, 'fixWpDoubleSlashFilter'], 10, 1);
124
        $this->getWpBridge()->addFilter('upload_dir', [$this, 'fixWpProtocolFilter'], 10, 1);
125
    }
126
127
    /**
128
     * Set wordpress folder name.
129
     *
130
     * @param string
131
     */
132
    protected function setWpFolderName()
133
    {
134
        $dirs = explode('/', $this->wpDirectoryPath);
135
136
        $this->wpFolderName = $dirs[count($dirs) - 2];
137
    }
138
}
139