Skip to content

Commit

Permalink
Merge pull request #23 from prisis/feature/new-Multisite-Directory-Re…
Browse files Browse the repository at this point in the history
…solver-Manager

Feature/new multisite directory resolver manager
  • Loading branch information
prisis committed Jul 23, 2015
2 parents 55a85b1 + c93a5a6 commit a24c39d
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 123 deletions.
2 changes: 0 additions & 2 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ return Symfony\CS\Config\Config::create()
'strict_param',
'-no_empty_lines_after_phpdocs',
'header_comment',
'newline_after_open_tag',
'phpdoc_order',
'phpdoc_var_to_type',
'-phpdoc_to_comment',
]
)
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ php:
- 7.0
- hhvm
- nightly
- hhvm-nightly

matrix:
allow_failures:
- php: 7.0
- php: nightly
- php: hhvm
- php: hhvm-nightly
fast_finish: true

sudo: false
Expand Down
100 changes: 100 additions & 0 deletions src/Gwa/AbstractResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace Gwa\Wordpress;

/**
* Wordpress Multisite fixer.
*
* @author Daniel Bannert <bannert@greatwhiteark.com>
* @copyright 2015 Great White Ark
*
* @link http://www.greatwhiteark.com
*
* @license MIT
*/

/**
* AbstractResolver.
*
* @author Daniel Bannert
*/
abstract class AbstractResolver
{
/**
* Folder path to wordpress, with trailing slash.
*
* @var string
*/
protected $wpDirectoryPath = '';

/**
* Wordpress folder name.
*
* @var string
*/
protected $wpFolderName = '';

/**
* MultisiteDirectoryResolver.
*
* @param string $wpdir
*/
public function __construct($wpdir)
{
$this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';
$this->setWpFolderName();
}

/**
* Set the right links in Adminbar.
*
* @param string $path
* @param string $scheme
*
* @return string
*/
public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin')
{
if (strpos($path, $this->wpDirectoryPath)) {
return $path;
}

$wordpressUrl = [
'/(wp-admin)/',
'/(wp-login\.php)/',
'/(wp-activate\.php)/',
'/(wp-signup\.php)/',
];

$multiSiteUrl = [
$this->wpFolderName.'/wp-admin',
$this->wpFolderName.'/wp-login.php',
$this->wpFolderName.'/wp-activate.php',
$this->wpFolderName.'/wp-signup.php',
];

return preg_replace($wordpressUrl, $multiSiteUrl, $path, 1);
}

/**
* Init all filter.
*/
public function init()
{
add_filter('network_admin_url', [$this, 'fixNetworkAdminUrlFilter'], 10, 2);

add_filter('script_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
add_filter('style_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
}

/**
* Set wordpress folder name.
*
* @param string
*/
protected function setWpFolderName()
{
$dirs = explode('/', $this->wpDirectoryPath);

$this->wpFolderName = $dirs[count($dirs) - 2];
}
}
41 changes: 41 additions & 0 deletions src/Gwa/Contracts/MultisiteDirectoryResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Gwa\Wordpress\Contracts;

/**
* Wordpress Multisite fixer.
*
* @author Daniel Bannert <bannert@greatwhiteark.com>
* @copyright 2015 Great White Ark
*
* @link http://www.greatwhiteark.com
*
* @license MIT
*/

/**
* MultisiteDirectoryResolver.
*
* @author Daniel Bannert
*/
Interface MultisiteDirectoryResolver
{
/**
* Set the right links in Adminbar.
*
* @param string $path
* @param string $scheme
*
* @return string
*/
public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin');

/**
* Set the right path for script and style loader.
*
* @param string $src
* @param string $handle
*
* @return string
*/
public function fixStyleScriptPathFilter($src, $handle);
}
76 changes: 12 additions & 64 deletions src/Gwa/MultisiteDirectoryResolver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Gwa\Wordpress;

/**
Expand All @@ -13,24 +12,26 @@
* @license MIT
*/

use Gwa\Wordpress\Contracts\MultisiteDirectoryResolver as ResolverContract;

/**
* MultisiteDirectoryResolver.
*
* @author Daniel Bannert
*/
class MultisiteDirectoryResolver
class MultisiteDirectoryResolver extends AbstractResolver implements ResolverContract
{
/**
* Folder path to wordpress, with trailing slash.
*
* @type string
* @var string
*/
protected $wpDirectoryPath = '';

/**
* Wordpress folder name.
*
* @type string
* @var string
*/
protected $wpFolderName = '';

Expand All @@ -41,46 +42,11 @@ class MultisiteDirectoryResolver
*/
public function __construct($wpdir)
{
if (!is_string($wpdir) || $wpdir === '') {
throw new \Exception('Please set the relative path to your Wordpress install folder.');
}

$this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';
$this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';;

$this->setWpFolderName();
}

/**
* Set the right links in Adminbar.
*
* @param string $path
* @param string $scheme
*
* @return string
*/
public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin')
{
if (strpos($path, $this->wpDirectoryPath)) {
return $path;
}

$wordpressUrl = [
'/(wp-admin)/',
'/(wp-login\.php)/',
'/(wp-activate\.php)/',
'/(wp-signup\.php)/',
];

$multiSiteUrl = [
$this->wpFolderName.'/wp-admin',
$this->wpFolderName.'/wp-login.php',
$this->wpFolderName.'/wp-activate.php',
$this->wpFolderName.'/wp-signup.php',
];

return preg_replace($wordpressUrl, $multiSiteUrl, $path, 1);
}

/**
* Set the right path for wp-login and wp-admin.
*
Expand All @@ -97,7 +63,7 @@ public function fixSiteUrlFilter($url, $path, $scheme)
}

$wordpressUrl = ['/(wp-login\.php)/', '/(wp-admin)/'];
$multiSiteUrl = [$this->wpDirectoryPath.'wp-login.php', $this->wpDirectoryPath.'wp-admin'];
$multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-login.php', trim($this->wpDirectoryPath, '/').'/wp-admin'];

return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1);
}
Expand All @@ -120,10 +86,10 @@ public function fixStyleScriptPathFilter($src, $handle)
strpos($src, $dir) === false
) {
$styleUrl = explode(site_url(), $src);
$src = site_url().'/'.$dir.$styleUrl[1];
$src = site_url().$dir.$styleUrl[1];
}

if (strpos($src, 'plugins') && strpos($src, '/app')) {
if (strpos($src, '/app')) {
$src = str_replace('//app', '/app', $src);
}

Expand All @@ -145,34 +111,16 @@ public function fixWpIncludeFolder($url, $path)
}

$wordpressUrl = ['/(wp-includes)/'];
$multiSiteUrl = [$this->wpDirectoryPath.'wp-includes'];
$multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-includes'];

return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1);
}

/**
* Init all filter.
*/
public function init()
{
add_filter('network_admin_url', [$this, 'fixNetworkAdminUrlFilter'], 10, 2);
add_filter('site_url', [$this, 'fixSiteUrlFilter'], 10, 3);

add_filter('script_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
add_filter('style_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2);
parent::init();

add_filter('site_url', [$this, 'fixSiteUrlFilter'], 10, 3);
add_filter('includes_url', [$this, 'fixWpIncludeFolder'], 10, 2);
}

/**
* Set wordpress folder name.
*
* @param string
*/
protected function setWpFolderName()
{
$folders = explode('/', $this->wpDirectoryPath);

$this->wpFolderName = $folders[count($folders) - 2];
}
}
63 changes: 63 additions & 0 deletions src/Gwa/MultisiteResolverManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Gwa\Wordpress;

/**
* Wordpress Multisite fixer.
*
* @author Daniel Bannert <bannert@greatwhiteark.com>
* @copyright 2015 Great White Ark
*
* @link http://www.greatwhiteark.com
*
* @license MIT
*/

/**
* MultisiteResolverManager.
*
* @author Daniel Bannert
*/
class MultisiteResolverManager
{
const TYPE_SUBDOMAIN = '\Gwa\Wordpress\MultisiteSubDomainResolver';
const TYPE_FOLDER = '\Gwa\Wordpress\MultisiteDirectoryResolver';

/**
* Resolver Handler
*
* @var \Gwa\Wordpress\Contracts\MultisiteDirectoryResolver
*/
protected $handler;

/**
* MultisiteResolverManager.
*
* @param string $wpdir
* @param string $multisiteDomainType
*/
public function __construct($wpdir, $multisiteDomainType) {
if (!is_string($wpdir) || $wpdir === '' || $wpdir === '/') {
throw new \Exception('Please set the relative path to your Wordpress install folder.');
}

$this->handler = new $multisiteDomainType($wpdir);
}

/**
* Get current Handler
*
* @return \Gwa\Wordpress\Contracts\MultisiteDirectoryResolver
*/
public function getHandler()
{
return $this->handler;
}

/**
* Init all filter.
*/
public function init()
{
$this->handler->init();
}
}
Loading

0 comments on commit a24c39d

Please sign in to comment.